********************************************************************************
* Created: December 2022
* Author: Michael Jensen
* Last edited: MJ, Feb 2023
* This file makes the AISD tables for our presentation (and future paper).
* The presentation is still going through many iterations, so it's changing a lot, needs cleaning.
********************************************************************************

* load in clearned dataset
use "${datapath}student_level_all_data.dta", clear

* keep only sample with outcome variable
keep if zmss_22~=.
keep if online==0
save temp, replace


************************* TABLE 4 **********************************************
* Balance of student characteristics (gradeschoolunit level)
********************************************************************************
use temp, clear

global controls1 age female white black hispanic native days_missed esl_ind speced freelunch

mata: mata clear
local i = 1
local j = 1

*retain variable labels
foreach v of var * {
	local l`v' : variable label `v'
	if `"`l`v''"' == "" {
		local l`v' "`v'"
	}
}

collapse (mean) age white black native hispanic female esl_ind speced freelunch days_missed grade3 grade4 grade5 grade6 grade7 grade8 treatment, by(gradeschoolunit)

*Reattach labels
foreach v of var * {
	label var `v' `"`l`v''"'
}

global DESCVARS1 age white black native hispanic female esl_ind speced freelunch days_missed grade3 grade4 grade5 grade6 grade7 grade8

* Differences
foreach var in $DESCVARS1 {
    quietly xi: reg `var' treatment
	outreg, keep(treatment) rtitle("`: var label `var''") stats(b) ///
		noautosumm store(row`i') starlevels(10 5 1) starloc(1)
	outreg, replay(diff) append(row`i') ctitles("",Difference) ///
		store(diff) note("")
	local ++i
}
outreg, replay(diff)
	
* Then Summary statistics
local count: word count $DESCVARS1
mat sumstat = J(`count',3,.)

local i = 1
foreach var in $DESCVARS1 {
    quietly: summarize `var'
	mat sumstat[`i',1] = r(N)
	quietly: summarize `var' if treatment ==0
	mat sumstat[`i',2] = r(mean)

	quietly: summarize `var' if treatment ==1
	mat sumstat[`i',3] = r(mean)
	local i = `i' + 1
}
frmttable, statmat(sumstat) store(sumstat) sfmt(g,f,f)
	
outreg, replay(sumstat) merge(diff)
outreg, replay(sumstat) nocenter note("") plain ctitles("", N, Control, Treatment, Diff) multicol(1,2,4)

outreg using "${outputpath}table4.tex", ///
    replay(sumstat) tex fragment replace
	
	
	
***************************** TABLE 5 ******************************************
* Treatment effects by grade, 3 different specifications
********************************************************************************
use temp, clear
		
	local gradecuts allgrades grade36 grade78 
	local cuts all
	global controls1 age female white black hispanic native days_missed esl_ind speced freelunch
	
	local i = 1
	foreach gvar of local gradecuts {
		label variable all "`: var label `gvar''"
		foreach var of local cuts {
			quietly xi: reg zmss_22 treatment if `var' == 1 & `gvar' == 1, cluster(gradeschoolunit) 
			outreg, keep(treatment) rtitle("`: var label `var''") se summstat(N) store(row`i') starlevels(10 5 1) starloc(1)
			outreg, replay(main) append(row`i') ctitles("", No Controls) store(main) note("")
			local ++i
		}

	}

	local i = 1
	foreach gvar of local gradecuts {
		label variable all "`: var label `gvar''"
		foreach var of local cuts {
			quietly xi: reg zmss_22 treatment i.teachergrade if `var' == 1 & `gvar' == 1, cluster(gradeschoolunit) 
			outreg, keep(treatment) rtitle("`: var label `var''") se summstat(N) store(row`i') starlevels(10 5 1) starloc(1)
			outreg, replay(main1) append(row`i') ctitles("", Grade FEs) store(main1) note("")
			local ++i
		}

	}

	local i = 1
	foreach gvar of local gradecuts {
		label variable all "`: var label `gvar''"
		foreach var of local cuts {
			quietly xi: reg zmss_22 treatment ${controls1} i.teachergrade if `var' == 1 & `gvar' == 1, cluster(gradeschoolunit) 
			outreg, keep(treatment) rtitle("`: var label `var''") se summstat(N) store(row`i') starlevels(10 5 1) starloc(1)
			outreg, replay(main2) append(row`i') ctitles("", Grade FEs/Student Controls) store(main2) note("")
			local ++i
		}

	}

	outreg, replay(main) merge(main1)
outreg, replay(main) merge(main2)
	
	outreg using "${outputpath}table5.tex", ///
    replay(main) tex fragment replace


	
	
****************************** TABLE 6 *****************************************

**************************** TABLE 7 *******************************************
* 3 specificationas from last table, now broken out by demographics x grade groups
********************************************************************************
use temp, clear
local controls1 "age female white black hispanic native days_missed esl_ind speced freelunch"
gen nonhisp = -hispanic + 1	
gen low_dm = 0
replace low_dm = 1 if days_missed <= 5
gen high_dm = 0
replace high_dm = 1 if days_missed > 20
gen med_dm = 0
replace med_dm = 1 if low_dm == 0 & high_dm == 0
gen noesl = -esl_ind +1

label variable all 						"Full Sample"
label variable female 					"Female"
label variable male 					"Male"
label variable white 					"Race: White"
label variable black 					"Race: Black"
label variable hispanic 				"Ethnicity: Hispanic"
label variable nonhisp 					"Ethnicity: Not Hispanic"
label variable speced 					"Special Ed students"
label variable nonspec 					"Non-Special Ed"
label variable freelunch 				"Free lunch Eligible"
label variable nofreelunch 				"Non-free lunch"
label variable low_dm					"Days Missed \< 5"
label variable med_dm					"Days Missed 6-20"
label variable high_dm					"Days Missed \> 20"
label variable esl_ind					"ESL"
label variable noesl					"Non ESL"


local hetlist all female male white black hispanic nonhisp speced nonspec freelunch nofreelunch low_dm med_dm high_dm esl_ind noesl
foreach v of local hetlist {
	quietly xi: reg zmss_22 treatment if `v' == 1 & grade36 == 1, cluster(gradeschoolunit) 
		local t = _b[treatment]/_se[treatment]
		local p =2*ttail(e(df_r),abs(`t'))
		loc star "" 
		if `p' < 0.01 {
			loc star "***"
		}
		if `p' <0.05 & `p' > 0.01 { 
			loc star "**"
		}
		if `p' < 0.1 & `p' > 0.05 {
			loc star "*"
		}
		local `v'1_b = string(_b[treatment],"%9.3f") + "`star'"
		local `v'1_se = "(" + string(_se[treatment], "%9.3f") + ")"
		local `v'1_n = e(N)
		
	quietly xi: reg zmss_22 treatment i.teachergrade if `v' == 1 & grade36 == 1, cluster(gradeschoolunit) 
		local t = _b[treatment]/_se[treatment]
		local p =2*ttail(e(df_r),abs(`t'))
		loc star "" 
		if `p' < 0.01 {
			loc star "***"
		}
		if `p' <0.05 & `p' > 0.01 { 
			loc star "**"
		}
		if `p' < 0.1 & `p' > 0.05 {
			loc star "*"
		}
		local `v'2_b = string(_b[treatment],"%9.3f") + "`star'"
		local `v'2_se = "(" + string(_se[treatment], "%9.3f") + ")"
		
	xi: reg zmss_22 treatment $controls1 i.teachergrade if `v' == 1 & grade36 == 1, cluster(gradeschoolunit) 
		local t = _b[treatment]/_se[treatment]
		local p =2*ttail(e(df_r),abs(`t'))
		loc star "" 
		if `p' < 0.01 {
			loc star "***"
		}
		if `p' <0.05 & `p' > 0.01 { 
			loc star "**"
		}
		if `p' < 0.1 & `p' > 0.05 {
			loc star "*"
		}
		local `v'3_b = string(_b[treatment],"%9.3f") + "`star'"
		local `v'3_se = "(" + string(_se[treatment], "%9.3f") + ")"
}

	
* CHOW TESTS *
local ftestlist female hispanic speced freelunch esl_ind
foreach fvar of local ftestlist {	
	gen i`fvar' = treatment * `fvar'
	quietly xi: reg zmss_22 treatment i`fvar' `fvar' if grade36 == 1, cluster(gradeschoolunit) 
		test _b[i`fvar']=0 /*, notest
		test _b[`fvar']=0, accum*/
		local f = r(F)
		local p = r(p)
		loc star "" 
		if `p' < 0.01 {
			loc star "***"
		}
		if `p' <0.05 & `p' > 0.01 { 
			loc star "**"
		}
		if `p' < 0.1 & `p' > 0.05 {
			loc star "*"
		}
		local f`fvar'1 = string(`p',"%9.3f") + "`star'"
		
	quietly xi: reg zmss_22 treatment i`fvar' `fvar' i.teachergrade if grade36 == 1, cluster(gradeschoolunit) 
		test _b[i`fvar']=0 /*, notest
		test _b[`fvar']=0, accum*/
		local f = r(F)
		local p = r(p)
		loc star "" 
		if `p' < 0.01 {
			loc star "***"
		}
		if `p' <0.05 & `p' > 0.01 { 
			loc star "**"
		}
		if `p' < 0.1 & `p' > 0.05 {
			loc star "*"
		}
		local f`fvar'2 = string(`p',"%9.3f") + "`star'"
		
	quietly xi: reg zmss_22 treatment i`fvar' `fvar' $controls1 i.teachergrade if grade36 == 1, cluster(gradeschoolunit) 
		test _b[i`fvar']=0 /*, notest
		test _b[`fvar']=0, accum*/
		local f = r(F)
		local p = r(p)
		loc star "" 
		if `p' < 0.01 {
			loc star "***"
		}
		if `p' <0.05 & `p' > 0.01 { 
			loc star "**"
		}
		if `p' < 0.1 & `p' > 0.05 {
			loc star "*"
		}
		local f`fvar'3 = string(`p',"%9.3f") + "`star'"
}

* CHOW TESTS RACE - We do race separately since there are techincally more than 2 (drop all except white and black first)*
local ftestlist white
preserve
drop if white == 0 & black == 0
foreach fvar of local ftestlist {	
	gen i`fvar' = treatment * `fvar'
	quietly xi: reg zmss_22 treatment i`fvar' `fvar' if grade36 == 1, cluster(gradeschoolunit) 
		test _b[i`fvar']=0 /*, notest
		test _b[`fvar']=0, accum*/
		local f = r(F)
		local p = r(p)
		loc star "" 
		if `p' < 0.01 {
			loc star "***"
		}
		if `p' <0.05 & `p' > 0.01 { 
			loc star "**"
		}
		if `p' < 0.1 & `p' > 0.05 {
			loc star "*"
		}
		local f`fvar'1 = string(`p',"%9.3f") + "`star'"
		
	quietly xi: reg zmss_22 treatment i`fvar' `fvar' i.teachergrade if grade36 == 1, cluster(gradeschoolunit) 
		test _b[i`fvar']=0 /*, notest
		test _b[`fvar']=0, accum*/
		local f = r(F)
		local p = r(p)
		loc star "" 
		if `p' < 0.01 {
			loc star "***"
		}
		if `p' <0.05 & `p' > 0.01 { 
			loc star "**"
		}
		if `p' < 0.1 & `p' > 0.05 {
			loc star "*"
		}
		local f`fvar'2 = string(`p',"%9.3f") + "`star'"
		
	quietly xi: reg zmss_22 treatment i`fvar' `fvar' $controls1 i.teachergrade if grade36 == 1, cluster(gradeschoolunit) 
		test _b[i`fvar']=0 /*, notest
		test _b[`fvar']=0, accum*/
		local f = r(F)
		local p = r(p)
		loc star "" 
		if `p' < 0.01 {
			loc star "***"
		}
		if `p' <0.05 & `p' > 0.01 { 
			loc star "**"
		}
		if `p' < 0.1 & `p' > 0.05 {
			loc star "*"
		}
		local f`fvar'3 = string(`p',"%9.3f") + "`star'"
}

restore
local ftestlist low_dm
drop if low_dm == 0 & high_dm == 0
foreach fvar of local ftestlist {	
	gen i`fvar' = treatment * `fvar'
	quietly xi: reg zmss_22 treatment i`fvar' `fvar' if grade36 == 1, cluster(gradeschoolunit) 
		test _b[i`fvar']=0 /*, notest
		test _b[`fvar']=0, accum*/
		local f = r(F)
		local p = r(p)
		loc star "" 
		if `p' < 0.01 {
			loc star "***"
		}
		if `p' <0.05 & `p' > 0.01 { 
			loc star "**"
		}
		if `p' < 0.1 & `p' > 0.05 {
			loc star "*"
		}
		local f`fvar'1 = string(`p',"%9.3f") + "`star'"
		
	quietly xi: reg zmss_22 treatment i`fvar' `fvar' i.teachergrade if grade36 == 1, cluster(gradeschoolunit) 
		test _b[i`fvar']=0 /*, notest
		test _b[`fvar']=0, accum*/
		local f = r(F)
		local p = r(p)
		loc star "" 
		if `p' < 0.01 {
			loc star "***"
		}
		if `p' <0.05 & `p' > 0.01 { 
			loc star "**"
		}
		if `p' < 0.1 & `p' > 0.05 {
			loc star "*"
		}
		local f`fvar'2 = string(`p',"%9.3f") + "`star'"
		
	quietly xi: reg zmss_22 treatment i`fvar' `fvar' $controls1 i.teachergrade if grade36 == 1, cluster(gradeschoolunit) 
		test _b[i`fvar']=0 /*, notest
		test _b[`fvar']=0, accum*/
		local f = r(F)
		local p = r(p)
		loc star "" 
		if `p' < 0.01 {
			loc star "***"
		}
		if `p' <0.05 & `p' > 0.01 { 
			loc star "**"
		}
		if `p' < 0.1 & `p' > 0.05 {
			loc star "*"
		}
		local f`fvar'3 = string(`p',"%9.3f") + "`star'"
}


local label1 = "No Controls"
local label2 = "Grade FEs"
local label3 = "Grade FEs w/ Controls"
	
capture file close outfile
	file open outfile using "${outputpath}table7.tex", write replace
		file write outfile "\begin{tabular}{lccc}" _n
		** Create Headings
		file write outfile "\hline & I & II & III \\" _n
		file write outfile " & No Controls & Grade FEs & Grade FEs w/ Controls \\ \hline"  _n
		
		foreach v in all {
			file write outfile " `: var label `v'' (N=``v'1_n') & ``v'1_b' & ``v'2_b' & ``v'3_b' \\"  _n
			file write outfile " & ``v'1_se' & ``v'2_se' & ``v'3_se' \\ \hline "  _n
		}
		
		foreach v in female male {
			file write outfile " `: var label `v'' (N=``v'1_n') & ``v'1_b' & ``v'2_b' & ``v'3_b' \\"  _n
			file write outfile " & ``v'1_se' & ``v'2_se' & ``v'3_se' \\"  _n
		}
			file write outfile "P-value & `ffemale1' & `ffemale2' & `ffemale3' \\ \hline" _n
			
		foreach v in white black {
			file write outfile " `: var label `v'' (N=``v'1_n') & ``v'1_b' & ``v'2_b' & ``v'3_b' \\"  _n
			file write outfile " & ``v'1_se' & ``v'2_se' & ``v'3_se' \\"  _n
		}
			file write outfile "P-value & `fwhite1' & `fwhite2' & `fwhite3' \\ \hline" _n
			
		foreach v in hispanic nonhisp {
			file write outfile " `: var label `v'' (N=``v'1_n') & ``v'1_b' & ``v'2_b' & ``v'3_b' \\"  _n
			file write outfile " & ``v'1_se' & ``v'2_se' & ``v'3_se' \\"  _n
		}
			file write outfile "P-value & `fhispanic1' & `fhispanic2' & `fhispanic3' \\ \hline" _n
			
		foreach v in low_dm med_dm high_dm {
			file write outfile " `: var label `v'' (N=``v'1_n') & ``v'1_b' & ``v'2_b' & ``v'3_b' \\"  _n
			file write outfile " & ``v'1_se' & ``v'2_se' & ``v'3_se' \\"  _n
		}
			file write outfile "P-value & `flow_dm1' & `flow_dm2' & `flow_dm3' \\ \hline" _n
			
		foreach v in speced nonspec {
			file write outfile " `: var label `v'' (N=``v'1_n') & ``v'1_b' & ``v'2_b' & ``v'3_b' \\"  _n
			file write outfile " & ``v'1_se' & ``v'2_se' & ``v'3_se' \\"  _n
		}
			file write outfile "P-value & `fspeced1' & `fspeced2' & `fspeced3' \\ \hline" _n
			
		foreach v in freelunch nofreelunch {
			file write outfile " `: var label `v'' (N=``v'1_n') & ``v'1_b' & ``v'2_b' & ``v'3_b' \\"  _n
			file write outfile " & ``v'1_se' & ``v'2_se' & ``v'3_se' \\"  _n
		}
			file write outfile "P-value & `ffreelunch1' & `ffreelunch2' & `ffreelunch3' \\ \hline" _n
			
		foreach v in esl_ind noesl {
			file write outfile " `: var label `v'' (N=``v'1_n') & ``v'1_b' & ``v'2_b' & ``v'3_b' \\"  _n
			file write outfile " & ``v'1_se' & ``v'2_se' & ``v'3_se' \\"  _n
		}
			file write outfile "P-value & `fesl_ind1' & `fesl_ind2' & `fesl_ind3' \\ \hline" _n
			
		file write outfile "\hline \end{tabular}"
	file close outfile	
	
	
****************************** TABLE 8 *****************************************
* Quasi experimental analysis
********************************************************************************
use temp, clear
local min_levels class_minutes1099 class_minutes1599 class_minutes2099 class_minutes2599 class_minutes3099 class_minutes3599 class_minutes4099 class_minutes4599 class_minutes5099 class_minutes5599 class_minutes6099
gen varinterest =.
local gradecuts allgrades grade36 //grade78

local gradecuts2 allgrades grade36
local min_levels2 class_minutes1099 class_minutes1599 class_minutes2099 class_minutes2599 class_minutes3099


	local i = 1
	foreach l of local min_levels {
		replace varinterest = `l'
		qui xi: reg zmss_22 varinterest zmss_21 $controls1 grade3 grade4 grade5 grade6 grade7 grade8, cluster(gradeschoolunit)
		outreg, keep(varinterest) rtitles("`l'") se summstat() store(row`i') starlevels(10 5 1) starloc(1)
		outreg, replay(quasi1) append(row`i') ctitles("", I) store(quasi1) note("")
			local ++i
	}
	
	local i = 1
		foreach l of local min_levels {
		replace varinterest = `l'
		qui xi: reg zmss_22 varinterest zmss_21 $controls1 grade3 grade4 grade5 grade6 grade7 grade8 if grade36 == 1, cluster(gradeschoolunit)
		outreg, keep(varinterest) rtitles("`l'") se summstat() store(row`i') starlevels(10 5 1) starloc(1)
		outreg, replay(quasi2) append(row`i') ctitles("", II) store(quasi2) note("")
			local ++i
	}

	local i = 1
	*Table showing same info
	foreach l of local min_levels2 {
		replace varinterest = `l'
		qui xi: reg zmss_22 varinterest zmss_21 $controls1 grade3 grade4 grade5 grade6 grade7 grade8 if grade78 == 1, cluster(gradeschoolunit)
		outreg, keep(varinterest) rtitles("`l'") se summstat() store(row`i') starlevels(10 5 1) starloc(1)
		outreg, replay(quasi3) append(row`i') ctitles("", III) store(quasi3) note("")
			local ++i
	}
	
	mat bonus = J(24,1,.)
	
	forvalues i = 1(1)24 {
	    mat bonus[`i',1] = 0
	}

	frmttable, statmat(bonus) store(bonus) sfmt(g,f)
	outreg, replay(quasi3) append(bonus) rtitles("class_minutes1099" \ "" \ "R2"  \"N" \ "class_minutes1599" \ "" \ "R2" \ "N" \ "class_minutes2099" \ "" \ "R2" \ "N" \ "class_minutes2599" \ "" \ "R2" \ "N" \ "class_minutes3099" \ "" \ "R2" \ "N" \ "class_minutes3599" \ "" \ "R2" \ "N" \ "class_minutes4099" \ "" \ "R2" \ "N" \ "class_minutes4599" \ "" \ "R2" \ "N" \ "class_minutes5099" \ "" \ "R2" \ "N" \ "class_minutes5599" \ "" \ "R2" \ "N" \ "class_minutes6099" \ "" \ "R2" \ "N")

	outreg, replay(quasi1) merge(quasi2)
	outreg, replay(quasi1) merge(quasi3)

	outreg using "${outputpath}table8.tex", ///
    replay(quasi1) tex fragment replace
	
	

****************************** TABLE 9 *****************************************
* Quasi experimental analysis, grade school fixed effects
********************************************************************************
use temp, clear
local min_levels class_minutes1099 class_minutes1599 class_minutes2099 class_minutes2599 class_minutes3099 class_minutes3599 class_minutes4099 class_minutes4599 class_minutes5099 class_minutes5599 class_minutes6099
gen varinterest =.
local gradecuts allgrades grade36 //grade78

local gradecuts2 allgrades grade36
local min_levels2 class_minutes1099 class_minutes1599 class_minutes2099 class_minutes2599 class_minutes3099


	local i = 1
	foreach l of local min_levels {
		replace varinterest = `l'
		qui xi: reg zmss_22 varinterest zmss_21 $controls1 , cluster(gradeschoolunit) absorb(gradeschoolunit)
		outreg, keep(varinterest) rtitles("`l'") se summstat() store(rrow`i') starlevels(10 5 1) starloc(1)
		outreg, replay(qqua1) append(rrow`i') ctitles("", I) store(qqua1) note("")
			local ++i
	}
	
	local i = 1
		foreach l of local min_levels {
		replace varinterest = `l'
		qui xi: reg zmss_22 varinterest zmss_21 $controls1 if grade36 == 1, cluster(gradeschoolunit) absorb(gradeschoolunit)
		outreg, keep(varinterest) rtitles("`l'") se summstat() store(rrow`i') starlevels(10 5 1) starloc(1)
		outreg, replay(qqua2) append(rrow`i') ctitles("", II) store(qqua2) note("")
			local ++i
	}

	local i = 1
	*Table showing same info
	foreach l of local min_levels2 {
		replace varinterest = `l'
		qui xi: reg zmss_22 varinterest zmss_21 $controls1 if grade78 == 1, cluster(gradeschoolunit) absorb(gradeschoolunit)
		outreg, keep(varinterest) rtitles("`l'") se summstat() store(rrow`i') starlevels(10 5 1) starloc(1)
		outreg, replay(qqua3) append(rrow`i') ctitles("", III) store(qqua3) note("")
			local ++i
	}
	
	mat bonus = J(24,1,.)
	
	forvalues i = 1(1)24 {
	    mat bonus[`i',1] = 0
	}

	frmttable, statmat(bonus) store(bonus) sfmt(g,f)
	outreg, replay(qqua3) append(bonus) rtitles("class_minutes1099" \ "" \ "R2"  \"N" \ "class_minutes1599" \ "" \ "R2" \ "N" \ "class_minutes2099" \ "" \ "R2" \ "N" \ "class_minutes2599" \ "" \ "R2" \ "N" \ "class_minutes3099" \ "" \ "R2" \ "N" \ "class_minutes3599" \ "" \ "R2" \ "N" \ "class_minutes4099" \ "" \ "R2" \ "N" \ "class_minutes4599" \ "" \ "R2" \ "N" \ "class_minutes5099" \ "" \ "R2" \ "N" \ "class_minutes5599" \ "" \ "R2" \ "N" \ "class_minutes6099" \ "" \ "R2" \ "N")

	outreg, replay(qqua1) merge(qqua2)
	outreg, replay(qqua1) merge(qqua3)

	outreg using "${outputpath}table9.tex", ///
    replay(qqua1) tex fragment replace



**************************** TABLE 10 ******************************************
use temp, clear
local min_levels class_lu0199 class_lu0299 class_lu0399 class_lu0499 class_lu0599
gen varinterest =.
local gradecuts allgrades grade36 //grade78

*Table of same graph
local gradecuts2 allgrades grade36
local min_levels2 class_lu0199 class_lu0299

	local i = 1
	foreach l of local min_levels {
		replace varinterest = `l'
		qui xi: reg zmss_22 varinterest zmss_21 $controls1 grade3 grade4 grade5 grade6 grade7 grade8, cluster(gradeschoolunit)
		outreg, keep(varinterest) rtitles("`l'") se summstat() store(erow`i') starlevels(10 5 1) starloc(1)
		outreg, replay(quasi1b) append(erow`i') ctitles("", I) store(quasi1b) note("")
			local ++i
	}
	
	local i = 1
		foreach l of local min_levels {
		replace varinterest = `l'
		qui xi: reg zmss_22 varinterest zmss_21 $controls1 grade3 grade4 grade5 grade6 grade7 grade8 if grade36 == 1, cluster(gradeschoolunit)
		outreg, keep(varinterest) rtitles("`l'") se summstat() store(erow`i') starlevels(10 5 1) starloc(1)
		outreg, replay(quasi2b) append(erow`i') ctitles("", II) store(quasi2b) note("")
			local ++i
	}

	local i = 1
	*Table showing same info
	foreach l of local min_levels2 {
		replace varinterest = `l'
		qui xi: reg zmss_22 varinterest zmss_21 $controls1 grade3 grade4 grade5 grade6 grade7 grade8 if grade78 == 1, cluster(gradeschoolunit)
		outreg, keep(varinterest) rtitles("`l'") se summstat() store(erow`i') starlevels(10 5 1) starloc(1)
		outreg, replay(quasi3b) append(erow`i') ctitles("", III) store(quasi3b) note("")
			local ++i
	}
	
	mat bonusb = J(12,1,.)
	
	forvalues i = 1(1)12 {
	    mat bonusb[`i',1] = 0
	}

	frmttable, statmat(bonusb) store(bonusb) sfmt(g,f)
	outreg, replay(quasi3b) append(bonusb) rtitles("class_lu0199" \ "" \ "R2"  \"N" \ "class_lu0299" \ "" \ "R2" \ "N" \ "class_lu0399" \ "" \ "R2" \ "N" \ "class_lu0499" \ "" \ "R2" \ "N" \ "class_lu0599" \ "" \ "R2" \ "N")

	outreg, replay(quasi1b) merge(quasi2b)
	outreg, replay(quasi1b) merge(quasi3b)

	outreg using "${outputpath}table10.tex", ///
    replay(quasi1b) tex fragment replace



***************************** TABLE 11 *****************************************
*Column 1
use temp, clear

gen total_minutes0005 = total_minutes>=0 & total_minutes<5
gen total_minutes0525 = total_minutes>=5 & total_minutes<25
gen total_minutes2550 = total_minutes>=25 & total_minutes<50
gen total_minutes5099 = total_minutes>=50 & total_minutes<1000
label variable total_minutes0005 "0-5 minutes"
label variable total_minutes0525 "5-25 minutes"
label variable total_minutes2550 "25-50 minutes"
label variable total_minutes5099 "50+ minutes"

local gradecuts grade36 
local regvar total_minutes0525 total_minutes2550 total_minutes5099

local i = 1
foreach gvar of local gradecuts {
	foreach var of local regvar {	
			xi: areg zmss_22 total_minutes0525 total_minutes2550 total_minutes5099 zmss_21 $controls1 i.teachergrade if `gvar' == 1, cluster(gradeschoolunit) absorb(new_t_id)
		outreg, keep(`var') rtitles("`: var label `var''") se summstat(N) store(row`i') starlevels(10 5 1) starloc(1)
		outreg, replay(q2) append(row`i') ctitles("", III) store(q2) note("")
		local ++i
	}
}	
		
outreg using "${outputpath}table11_panel1.tex", ///
replay(q2) tex fragment replace

* Column 2
use temp, clear

gen total_lu01 = msl_net>=0 & msl_net<1
gen total_lu12 = msl_net>=1 & msl_net<2
gen total_lu25 = msl_net>=2 & msl_net<5
gen total_lu59 = msl_net>=5 & msl_net<99

label variable total_lu01 "0-1 Level-ups"
label variable total_lu12 "1-2 Level-ups"
label variable total_lu25 "2-5 Level-ups"
label variable total_lu59 "5-9 Level-ups"

local gradecuts grade36
local regvar total_lu12 total_lu25 total_lu59
	
local i = 1
foreach gvar of local gradecuts {
	foreach var of local regvar {	
			xi: areg zmss_22 total_lu12 total_lu25 total_lu59 zmss_21 $controls1 i.teachergrade if `gvar' == 1, cluster(gradeschoolunit) absorb(new_t_id)
		outreg, keep(`var') rtitles("`: var label `var''") se summstat(N) store(row`i') starlevels(10 5 1) starloc(1)
		outreg, replay(qil2) append(row`i') ctitles("", III) store(qil2) note("")
		local ++i
	}
}	
		
outreg using "${outputpath}table11_panel2.tex", ///
replay(qil2) tex fragment replace
	
	
*Column 3

use temp, clear

gen total_minutes0005 = total_minutes>=0 & total_minutes<5
gen total_minutes0525 = total_minutes>=5 & total_minutes<25
gen total_minutes2550 = total_minutes>=25 & total_minutes<50
gen total_minutes5099 = total_minutes>=50 & total_minutes<1000

gen total_lu01 = msl_net>=0 & msl_net<1
gen total_lu12 = msl_net>=1 & msl_net<2
gen total_lu25 = msl_net>=2 & msl_net<5
gen total_lu59 = msl_net>=5 & msl_net<99

label variable total_lu01 "0-1 Level-ups"
label variable total_lu12 "1-2 Level-ups"
label variable total_lu25 "2-5 Level-ups"
label variable total_lu59 "5-9 Level-ups"

label variable total_minutes0005 "0-5 minutes"
label variable total_minutes0525 "5-25 minutes"
label variable total_minutes2550 "25-50 minutes"
label variable total_minutes5099 "50+ minutes"

local gradecuts grade36
local regvar total_lu12 total_lu25 total_lu59 total_minutes0525 total_minutes2550 total_minutes5099 

local i = 1
foreach gvar of local gradecuts {
	foreach var of local regvar {	
			xi: areg zmss_22 total_lu12 total_lu25 total_lu59 total_minutes0525 total_minutes2550 total_minutes5099  zmss_21 $controls1 i.teachergrade if `gvar' == 1, cluster(gradeschoolunit) absorb(new_t_id)
		outreg, keep(`var') rtitles("`: var label `var''") se summstat(N) store(row`i') starlevels(10 5 1) starloc(1)
		outreg, replay(qib2) append(row`i') ctitles("", III) store(qib2) note("")
		local ++i
	}
}	

outreg using "${outputpath}table11_panel3.tex", ///
replay(qib2) tex fragment replace





******************************* TABLE 12 ***************************************

******************************* FIGURE 5 ***************************************
* FIGURE: Distribution of practice among treatment (Buckets)
use temp, clear

keep if treatment==1

histogram classminutes if classminutes<100, start(-0.5) width(1) xlabel(0(10)100) ylabel(0(5)30) percent name(graph1, replace) title("All Grades") xtitle("Minutes")
histogram classminutes if classminutes<100 & grade<=6, start(-0.5) width(1) xlabel(0(10)100) ylabel(0(5)30) percent name(graph2, replace) title("Grades 3-6") xtitle("Minutes")
histogram classminutes if classminutes<100 & grade>6, start(-0.5) width(1) xlabel(0(10)100) ylabel(0(5)30) percent name(graph3, replace) title("Grades 7-8") xtitle("Minutes")

graph combine graph1 graph2 graph3, rows(1) cols(3) xsize(10) title("Average Weekly Practice Time") subtitle("Distribution: Class Averages")

graph export "${outputpath}figure5_panel1.png", replace

histogram classlu if classlu<=5.5, start(-0.5) width(1) percent xlabel(0(1)5) ylabel(0(10)70) name(graph1, replace)  title("All Grades") xtitle("Skills Leveled-Up")
histogram classlu if classlu<=5.5 & grade<=6, start(-0.5) width(1) percent xlabel(0(1)5) ylabel(0(10)70) name(graph2, replace) title("Grades 3-6") xtitle("Skills Leveled-Up")
histogram classlu if classlu<=5.5 & grade>6, start(-0.5) width(1) percent xlabel(0(1)5) ylabel(0(10)70) name(graph3, replace) title("Grades 7-8") xtitle("Skills Leveled-Up")

graph combine graph1 graph2 graph3, rows(1) cols(3) xsize(10) title("Average Weekly Skills Leveled-Up") subtitle("Distribution: Class Averages")

graph export "${outputpath}figure5_panel2.png", replace




******************************** FIGURE 6 **************************************
use temp, clear
reg zmss_22 zmss_21 
predict resid_22
drop if classminutes > 110

npregress kernel resid_22 classminutes
npgraph

margins, at(classminutes=(0(10)110))
marginsplot




******************************** FIGURE 7 **************************************

********************************* FIGURE 8 *************************************

********************************* TABLE A2 *************************************

********************************** TABLE A3 ************************************

********************************** TABLE A4 ************************************

*********************************** TABLE A5 ***********************************

************************************* FIGURE A2 ********************************
use temp, clear

keep if treatment==0

histogram classminutes if classminutes<100, start(-0.5) width(1) xlabel(0(10)100) ylabel(0(5)30) percent name(graph1, replace) title("All Grades") xtitle("Minutes")
histogram classminutes if classminutes<100 & grade<=6, start(-0.5) width(1) xlabel(0(10)100) ylabel(0(5)30) percent name(graph2, replace) title("Grades 3-6") xtitle("Minutes")
histogram classminutes if classminutes<100 & grade>6, start(-0.5) width(1) xlabel(0(10)100) ylabel(0(5)30) percent name(graph3, replace) title("Grades 7-8") xtitle("Minutes")

graph combine graph1 graph2 graph3, rows(1) cols(3) xsize(10) title("Average Weekly Practice Time") subtitle("Distribution: Class Averages")

graph export "${outputpath}figureA2_panel1.png", replace

histogram classlu if classlu<=5.5, start(-0.5) width(1) percent xlabel(0(1)5) ylabel(0(10)70) name(graph1, replace)  title("All Grades") xtitle("Skills Leveled-Up")
histogram classlu if classlu<=5.5 & grade<=6, start(-0.5) width(1) percent xlabel(0(1)5) ylabel(0(10)70) name(graph2, replace) title("Grades 3-6") xtitle("Skills Leveled-Up")
histogram classlu if classlu<=5.5 & grade>6, start(-0.5) width(1) percent xlabel(0(1)5) ylabel(0(10)70) name(graph3, replace) title("Grades 7-8") xtitle("Skills Leveled-Up")

graph combine graph1 graph2 graph3, rows(1) cols(3) xsize(10) title("Average Weekly Skills Leveled-Up") subtitle("Distribution: Class Averages")

graph export "${outputpath}figureA2_panel2.png", replace


************************************* FIGURE A3 ********************************
use temp, clear

keep if treatment==1

histogram total_minutes if total_minutes<100, start(-0.5) width(1) ylabel(0(1)15) percent name(graph1, replace) title("All Grades") xtitle("Minutes")
histogram total_minutes if total_minutes<100 & grade<=6, start(-0.5) width(1)  ylabel(0(1)18) percent name(graph2, replace) title("Grades 3-6") xtitle("Minutes")
histogram total_minutes if total_minutes<100 & grade>6, start(-0.5) width(1)  ylabel(0(1)18) percent name(graph3, replace) title("Grades 7-8") xtitle("Minutes")

graph combine graph1 graph2 graph3, rows(1) cols(3) xsize(10) title("Average Weekly Practice Time") subtitle("Distribution: Individual")

graph export "${outputpath}figureA3_panel1.png", replace

histogram msl_net if msl_net<=5.5, start(-0.5) width(1) percent xlabel(0(1)5) ylabel(0(10)70) name(graph1, replace) title("All Grades") xtitle("Skills Leveled-Up")
histogram msl_net if msl_net<=5.5 & grade<=6, start(-0.5) width(1) percent xlabel(0(1)5) ylabel(0(10)70) name(graph2, replace) title("Grades 3-6") xtitle("Skills Leveled-Up")
histogram msl_net if msl_net<=5.5 & grade>6, start(-0.5) width(1) percent xlabel(0(1)5) ylabel(0(10)70) name(graph3, replace) title("Grades 7-8") xtitle("Skills Leveled-Up")

graph combine graph1 graph2 graph3, rows(1) cols(3) xsize(10) title("Average Weekly Skills Leveled-Up") subtitle("Distribution: Individual")

graph export "${outputpath}figureA3_panel2.png", replace


************************************ FIGURE A4 *********************************
use temp, clear

keep if treatment==0

histogram total_minutes if total_minutes<100, start(-0.5) width(1) ylabel(0(1)15) percent name(graph1, replace) title("All Grades") xtitle("Minutes")
histogram total_minutes if total_minutes<100 & grade<=6, start(-0.5) width(1)  ylabel(0(1)18) percent name(graph2, replace) title("Grades 3-6") xtitle("Minutes")
histogram total_minutes if total_minutes<100 & grade>6, start(-0.5) width(1)  ylabel(0(1)18) percent name(graph3, replace) title("Grades 7-8") xtitle("Minutes")

graph combine graph1 graph2 graph3, rows(1) cols(3) xsize(10) title("Average Weekly Practice Time") subtitle("Distribution: Individual")

graph export "${outputpath}figureA4_panel1.png", replace

histogram msl_net if msl_net<=5.5, start(-0.5) width(1) percent xlabel(0(1)5) ylabel(0(10)70) name(graph1, replace) title("All Grades") xtitle("Skills Leveled-Up")
histogram msl_net if msl_net<=5.5 & grade<=6, start(-0.5) width(1) percent xlabel(0(1)5) ylabel(0(10)70) name(graph2, replace) title("Grades 3-6") xtitle("Skills Leveled-Up")
histogram msl_net if msl_net<=5.5 & grade>6, start(-0.5) width(1) percent xlabel(0(1)5) ylabel(0(10)70) name(graph3, replace) title("Grades 7-8") xtitle("Skills Leveled-Up")

graph combine graph1 graph2 graph3, rows(1) cols(3) xsize(10) title("Average Weekly Skills Leveled-Up") subtitle("Distribution: Individual")

graph export "${outputpath}figureA4_panel2.png", replace


************************************* FIGURE A5 ********************************

************************************* FIGURE A6 ********************************

************************************** FIGURE A7 *******************************

************************************** FIGURE A8 *******************************



/*





















***************
use temp, clear
global controls1 age female white black hispanic native days_missed esl_ind speced freelunch
drop if classminutes == 0
gen class_eff = classlu / (classminutes/60)

replace class_eff = 0 if class_eff ==.

*drop if grade36 != 1
*drop if treatment == 0
gen inter_eff = treatment * class_eff





local min_levels class_minutes1099 class_minutes1599 class_minutes2099 class_minutes2599 class_minutes3099 class_minutes3599 class_minutes4099 class_minutes4599 class_minutes5099 class_minutes5599 class_minutes6099
gen varinterest =.
gen interaction =.
local gradecuts allgrades grade36 //grade78
local i = 1

local gradecuts2 allgrades grade36
local min_levels2 class_minutes1099 class_minutes1599 class_minutes2099 class_minutes2599 class_minutes3099


	local i = 1
	foreach l of local min_levels {
		replace varinterest = `l'
		replace interaction = varinterest * class_eff
		qui xi: reg zmss_22 varinterest interaction zmss_21 $controls1 grade3 grade4 grade5 grade6 grade7 grade8, cluster(gradeschoolunit)
		outreg, keep(varinterest interaction) rtitles("`l'") se summstat() store(row`i') starlevels(10 5 1) starloc(1)
		outreg, replay(quasi1) append(row`i') ctitles("", I) store(quasi1) note("")
			local ++i
	}
	
	local i = 1
		foreach l of local min_levels {
		replace varinterest = `l'
		replace interaction = varinterest * class_eff
		qui xi: reg zmss_22 varinterest interaction zmss_21 $controls1 grade3 grade4 grade5 grade6 grade7 grade8 if grade36 == 1, cluster(gradeschoolunit)
		outreg, keep(varinterest interaction) rtitles("`l'") se summstat() store(row`i') starlevels(10 5 1) starloc(1)
		outreg, replay(quasi2) append(row`i') ctitles("", II) store(quasi2) note("")
			local ++i
	}

	local i = 1
	*Table showing same info
	foreach l of local min_levels2 {
		replace varinterest = `l'
		replace interaction = varinterest * class_eff
		qui xi: reg zmss_22 varinterest interaction zmss_21 $controls1 grade3 grade4 grade5 grade6 grade7 grade8 if grade78 == 1, cluster(gradeschoolunit)
		outreg, keep(varinterest interaction) rtitles("`l'") se summstat() store(row`i') starlevels(10 5 1) starloc(1)
		outreg, replay(quasi3) append(row`i') ctitles("", III) store(quasi3) note("")
			local ++i
	}
	
	mat bonus = J(36,1,.)
	
	forvalues i = 1(1)36 {
	    mat bonus[`i',1] = 0
	}

	frmttable, statmat(bonus) store(bonus) sfmt(g,f)
	outreg, replay(quasi3) append(bonus) rtitles("class_minutes1099" \ "" \ "R2"  \"N" \ "class_minutes1599" \ "" \ "R2" \ "N" \ "class_minutes2099" \ "" \ "R2" \ "N" \ "class_minutes2599" \ "" \ "R2" \ "N" \ "class_minutes3099" \ "" \ "R2" \ "N" \ "class_minutes3599" \ "" \ "R2" \ "N" \ "class_minutes4099" \ "" \ "R2" \ "N" \ "class_minutes4599" \ "" \ "R2" \ "N" \ "class_minutes5099" \ "" \ "R2" \ "N" \ "class_minutes5599" \ "" \ "R2" \ "N" \ "class_minutes6099" \ "" \ "R2" \ "N")

	outreg, replay(quasi1) merge(quasi2)
	*outreg, replay(quasi1) merge(quasi3)

	outreg using "${outputpath}final_figures/quasi_graph_w_eff_inter.tex", ///
    replay(quasi1) tex fragment replace







*Quasi experimental analysis


* first look at student individual minutes


use temp, clear

gen total_minutes0005 = total_minutes>=0 & total_minutes<5
gen total_minutes0525 = total_minutes>=5 & total_minutes<25
gen total_minutes2550 = total_minutes>=25 & total_minutes<50
gen total_minutes5099 = total_minutes>=50 & total_minutes<1000
label variable total_minutes0005 "0-5 minutes"
label variable total_minutes0525 "5-25 minutes"
label variable total_minutes2550 "25-50 minutes"
label variable total_minutes5099 "50+ minutes"

local gradecuts all grade36 grade78
local regvar total_minutes0525 total_minutes2550 total_minutes5099
	
	
	
local i = 1
foreach gvar of local gradecuts {
	foreach var of local regvar {	
		quietly reg zmss_22 total_minutes0525 total_minutes2550 total_minutes5099 zmss_21 $controls1 i.teachergrade if `gvar' == 1, cluster(gradeschoolunit) 
		outreg, keep(`var') rtitles("`: var label `var''") se summstat(N) store(row`i') starlevels(10 5 1) starloc(1)
		outreg, replay(q) append(row`i') ctitles("", I) store(q) note("")
		local ++i
	}
}	


local i = 1
foreach gvar of local gradecuts {
	foreach var of local regvar {	
			xi: areg zmss_22 total_minutes0525 total_minutes2550 total_minutes5099 zmss_21 $controls1 i.teachergrade if `gvar' == 1, cluster(gradeschoolunit) absorb(teacherschool)
		outreg, keep(`var') rtitles("`: var label `var''") se summstat(N) store(row`i') starlevels(10 5 1) starloc(1)
		outreg, replay(q1) append(row`i') ctitles("", II) store(q1) note("")
		local ++i
	}
}	

local i = 1
foreach gvar of local gradecuts {
	foreach var of local regvar {	
			xi: areg zmss_22 total_minutes0525 total_minutes2550 total_minutes5099 zmss_21 $controls1 i.teachergrade if `gvar' == 1, cluster(gradeschoolunit) absorb(gradeschoolunit)
		outreg, keep(`var') rtitles("`: var label `var''") se summstat(N) store(row`i') starlevels(10 5 1) starloc(1)
		outreg, replay(q2) append(row`i') ctitles("", III) store(q2) note("")
		local ++i
	}
}	

	outreg, replay(q) merge(q1)
	outreg, replay(q) merge(q2)
		
	
	outreg using "${outputpath}final_figures/quasi_individual_min.tex", ///
    replay(q) tex fragment replace note("Specification I: Controls for student demographics and 2021 STAAR Math Scores, Grade FEs. SEs clustered at the grade-school level. Specification II: Adds School FEs. Specification III: Adds Grade-School FEs. * indicate significance at the .1, .05, and .01 levels.")
	
	

* individual level ups


use temp, clear

gen total_lu01 = msl_net>=0 & msl_net<1
gen total_lu12 = msl_net>=1 & msl_net<2
gen total_lu25 = msl_net>=2 & msl_net<5
gen total_lu59 = msl_net>=5 & msl_net<99

label variable total_lu01 "0-1 Level-ups"
label variable total_lu12 "1-2 Level-ups"
label variable total_lu25 "2-5 Level-ups"
label variable total_lu59 "5-9 Level-ups"

local gradecuts all grade36 grade78
local regvar total_lu12 total_lu25 total_lu59
	
	
	
local i = 1
foreach gvar of local gradecuts {
	foreach var of local regvar {	
		quietly reg zmss_22 total_lu12 total_lu25 total_lu59 zmss_21 $controls1 i.teachergrade if `gvar' == 1, cluster(gradeschoolunit) 
		outreg, keep(`var') rtitles("`: var label `var''") se summstat(N) store(row`i') starlevels(10 5 1) starloc(1)
		outreg, replay(qil) append(row`i') ctitles("", I) store(qil) note("")
		local ++i
	}
}	


local i = 1
foreach gvar of local gradecuts {
	foreach var of local regvar {	
			xi: areg zmss_22 total_lu12 total_lu25 total_lu59 zmss_21 $controls1 i.teachergrade if `gvar' == 1, cluster(gradeschoolunit) absorb(teacherschool)
		outreg, keep(`var') rtitles("`: var label `var''") se summstat(N) store(row`i') starlevels(10 5 1) starloc(1)
		outreg, replay(qil1) append(row`i') ctitles("", II) store(qil1) note("")
		local ++i
	}
}	

local i = 1
foreach gvar of local gradecuts {
	foreach var of local regvar {	
			xi: areg zmss_22 total_lu12 total_lu25 total_lu59 zmss_21 $controls1 i.teachergrade if `gvar' == 1, cluster(gradeschoolunit) absorb(gradeschoolunit)
		outreg, keep(`var') rtitles("`: var label `var''") se summstat(N) store(row`i') starlevels(10 5 1) starloc(1)
		outreg, replay(qil2) append(row`i') ctitles("", III) store(qil2) note("")
		local ++i
	}
}	

	outreg, replay(qil) merge(qil1)
	outreg, replay(qil) merge(qil2)
		
	
	outreg using "${outputpath}final_figures/quasi_individual_lu.tex", ///
    replay(qil) tex fragment replace note("Specification I: Controls for student demographics and 2021 STAAR Math Scores, Grade FEs. SEs clustered at the grade-school level. Specification II: Adds School FEs. Specification III: Adds Grade-School FEs. * indicate significance at the .1, .05, and .01 levels.")
	
	
********************************************************************************
*TABLE: For full sample, a quasi regression with both level-ups and minutes

use temp, clear

gen total_minutes0005 = total_minutes>=0 & total_minutes<5
gen total_minutes0525 = total_minutes>=5 & total_minutes<25
gen total_minutes2550 = total_minutes>=25 & total_minutes<50
gen total_minutes5099 = total_minutes>=50 & total_minutes<1000

gen total_lu01 = msl_net>=0 & msl_net<1
gen total_lu12 = msl_net>=1 & msl_net<2
gen total_lu25 = msl_net>=2 & msl_net<5
gen total_lu59 = msl_net>=5 & msl_net<99

label variable total_lu01 "0-1 Level-ups"
label variable total_lu12 "1-2 Level-ups"
label variable total_lu25 "2-5 Level-ups"
label variable total_lu59 "5-9 Level-ups"

label variable total_minutes0005 "0-5 minutes"
label variable total_minutes0525 "5-25 minutes"
label variable total_minutes2550 "25-50 minutes"
label variable total_minutes5099 "50+ minutes"

local gradecuts all
local regvar total_lu12 total_lu25 total_lu59 total_minutes0525 total_minutes2550 total_minutes5099 



local i = 1
foreach gvar of local gradecuts {
	foreach var of local regvar {	
		quietly reg zmss_22 total_lu12 total_lu25 total_lu59 total_minutes0525 total_minutes2550 total_minutes5099  zmss_21 $controls1 i.teachergrade if `gvar' == 1, cluster(gradeschoolunit) 
		outreg, keep(`var') rtitles("`: var label `var''") se summstat(N) store(row`i') starlevels(10 5 1) starloc(1)
		outreg, replay(qib) append(row`i') ctitles("", I) store(qib) note("")
		local ++i
	}
}	


local i = 1
foreach gvar of local gradecuts {
	foreach var of local regvar {	
			xi: areg zmss_22 total_lu12 total_lu25 total_lu59 total_minutes0525 total_minutes2550 total_minutes5099  zmss_21 $controls1 i.teachergrade if `gvar' == 1, cluster(gradeschoolunit) absorb(teacherschool)
		outreg, keep(`var') rtitles("`: var label `var''") se summstat(N) store(row`i') starlevels(10 5 1) starloc(1)
		outreg, replay(qib1) append(row`i') ctitles("", II) store(qib1) note("")
		local ++i
	}
}	

local i = 1
foreach gvar of local gradecuts {
	foreach var of local regvar {	
			xi: areg zmss_22 total_lu12 total_lu25 total_lu59 total_minutes0525 total_minutes2550 total_minutes5099  zmss_21 $controls1 i.teachergrade if `gvar' == 1, cluster(gradeschoolunit) absorb(gradeschoolunit)
		outreg, keep(`var') rtitles("`: var label `var''") se summstat(N) store(row`i') starlevels(10 5 1) starloc(1)
		outreg, replay(qib2) append(row`i') ctitles("", III) store(qib2) note("")
		local ++i
	}
}	

	outreg, replay(qib) merge(qib1)
	outreg, replay(qib) merge(qib2)
		
	
	outreg using "${outputpath}final_figures/quasi_individual_both.tex", ///
    replay(qib) tex fragment replace

	
	
	
	
	
********************************************************************************
*TABLE: For full sample, a quasi regression with both level-ups and minutes for class averages

use temp, clear
gen class_minutes2535 = 0
replace class_minutes2535 = 1 if classminutes >= 25 & classminutes <= 35
gen class_lu0203 = 0
replace class_lu0203 = 1 if classlu >= 2 & classlu <= 3

label variable class_minutes2535 "25-35 minutes"
label variable class_minutes3599 "35+ minutes"
label variable class_lu0203 "2-3 Level-ups"
label variable class_lu0399 "3+ Level-ups"


local gradecuts all
local regvar class_minutes2535 class_minutes3599 class_lu0203 class_lu0399 

local i = 1
foreach gvar of local gradecuts {
	foreach var of local regvar {	
		quietly reg zmss_22 class_minutes2535 class_minutes3599 class_lu0203 class_lu0399 zmss_21 $controls1 i.teachergrade if `gvar' == 1, cluster(gradeschoolunit) 
		outreg, keep(`var') rtitles("`: var label `var''") se summstat(N) store(row`i') starlevels(10 5 1) starloc(1)
		outreg, replay(qcb) append(row`i') ctitles("", I) store(qcb) note("")
		local ++i
	}
}	


local i = 1
foreach gvar of local gradecuts {
	foreach var of local regvar {	
			xi: areg zmss_22 class_minutes2535 class_minutes3599 class_lu0203 class_lu0399 zmss_21 $controls1 i.teachergrade if `gvar' == 1, cluster(gradeschoolunit) absorb(teacherschool)
		outreg, keep(`var') rtitles("`: var label `var''") se summstat(N) store(row`i') starlevels(10 5 1) starloc(1)
		outreg, replay(qcb1) append(row`i') ctitles("", II) store(qcb1) note("")
		local ++i
	}
}	

local i = 1
foreach gvar of local gradecuts {
	foreach var of local regvar {	
			xi: areg zmss_22 class_minutes2535 class_minutes3599 class_lu0203 class_lu0399 zmss_21 $controls1 i.teachergrade if `gvar' == 1, cluster(gradeschoolunit) absorb(gradeschoolunit)
		outreg, keep(`var') rtitles("`: var label `var''") se summstat(N) store(row`i') starlevels(10 5 1) starloc(1)
		outreg, replay(qcb2) append(row`i') ctitles("", III) store(qcb2) note("")
		local ++i
	}
}	

	outreg, replay(qcb) merge(qcb1)
	outreg, replay(qcb) merge(qcb2)
		
	
	outreg using "${outputpath}final_figures/quasi_class_both.tex", ///
    replay(qcb) tex fragment replace



	
	




	
********************************************************************************
*FIGURE: Effect of practice time graphed for various practice times. Then broken out by grade
use temp, clear
local min_levels class_minutes1099 class_minutes1599 class_minutes2099 class_minutes2599 class_minutes3099 class_minutes3599 class_minutes4099 class_minutes4599 class_minutes5099 class_minutes5599 class_minutes6099
gen varinterest =.
local gradecuts allgrades grade36 /*grade78*/

local i = 1
foreach g of local gradecuts {
    local m = 5
	foreach l of local min_levels {
		local m = `m' + 5
		replace varinterest = `l'
		label variable varinterest "`m'"
		qui xi: reg zmss_22 varinterest zmss_21 $controls1 grade3 grade4 grade5 grade6 grade7 grade8 if `g' == 1, cluster(gradeschoolunit)
		estimates store m`m'
	}

	coefplot m10 || m15 || m20 || m25 || m30 || m35 || m40 || m45 || m50 || m55 || m60, keep(varinterest) vertical bycoefs title("`: var label `g''") xtitle("At least X Minutes") name(graph`i', replace) yline(0) xlab(1 "10" 2 "15" 3 "20" 4 "25" 5 "30" 6 "35" 7 "40" 8 "45" 9 "50" 10 "55" 11 "60") ylabel(,angle(horizontal) format(%3.1f))
	
	local ++i
}

local m = 5
foreach l of local min_levels {
	local m = `m' + 5
	replace varinterest = `l'
	label variable varinterest "`m'"
	qui xi: reg zmss_22 varinterest zmss_21 $controls1 grade3 grade4 grade5 grade6 grade7 grade8 if grade78 == 1, cluster(gradeschoolunit)
	estimates store m`m'
}

coefplot m10 || m15 || m20 || m25 || m30 || m35 || m40 || m45 || m50 || m55 || m60, keep(varinterest) vertical bycoefs title("`: var label grade78'") xtitle("At least X Minutes") name(graph3, replace) yline(0) xlab(1 "10" 2 "15" 3 "20" 4 "25" 5 "30") ylabel(,angle(horizontal) format(%3.1f))
	

graph combine graph1 graph2 graph3, rows(1) cols(3) xsize(10) title("Effect of classroom with at least X practice minutes") subtitle("Weekly Class Average")

graph export "${outputpath}final_figures/class_averages_effects.png", replace



********************************************************************************
*FIGURE: Same as above, with gradeschoolunit fixed affects
use temp, clear
local min_levels class_minutes1099 class_minutes1599 class_minutes2099 class_minutes2599 class_minutes3099 class_minutes3599 class_minutes4099 class_minutes4599 class_minutes5099 class_minutes5599 class_minutes6099
gen varinterest =.
local gradecuts allgrades grade36 //grade78
local i = 1
foreach g of local gradecuts {
    local m = 5
	foreach l of local min_levels {
		local m = `m' + 5
		replace varinterest = `l'
		xi: reg zmss_22 varinterest zmss_21 $controls1 if `g' == 1, cluster(gradeschoolunit) absorb(gradeschoolunit)
		estimates store m`m'
	}

	coefplot m10 || m15 || m20 || m25 || m30 || m35 || m40 || m45 || m50 || m55 || m60, drop(_cons zmss_21 age female white black hispanic native days_missed esl_ind speced freelunch) vertical bycoefs title("`: var label `g''") xtitle("At least X Minutes") name(graph`i', replace) yline(0) ylabel(,angle(horizontal) format(%3.1f)) xlab(1 "10" 2 "15" 3 "20" 4 "25" 5 "30" 6 "35" 7 "40" 8 "45" 9 "50" 10 "55" 11 "60")
	
	local ++i
}

local m = 5
foreach l of local min_levels {
	local m = `m' + 5
	replace varinterest = `l'
	label variable varinterest "`m'"
	qui xi: reg zmss_22 varinterest zmss_21 $controls1 if grade78 == 1, cluster(gradeschoolunit) absorb(gradeschoolunit)
	estimates store m`m'
}

coefplot m10 || m15 || m20 || m25 || m30, keep(varinterest) vertical bycoefs title("`: var label grade78'") xtitle("At least X Minutes") name(graph3, replace) yline(0) xlab(1 "10" 2 "15" 3 "20" 4 "25" 5 "30") ylabel(,angle(horizontal) format(%3.1f))



graph combine graph1 graph2 graph3, rows(1) cols(3) xsize(10) title("Effect of classroom with at least X practice minutes") subtitle("Weekly Class Average, w/ Grade-School FEs")

graph export "${outputpath}final_figures/class_averages_effects_w_fe.png", replace









local gradecuts2 allgrades grade36
local min_levels2 class_minutes1099 class_minutes1599 class_minutes2099 class_minutes2599 class_minutes3099


	local i = 1
	foreach l of local min_levels {
		replace varinterest = `l'
		qui xi: reg zmss_22 varinterest zmss_21 $controls1 grade3 grade4 grade5 grade6 grade7 grade8, cluster(gradeschoolunit)
		outreg, keep(varinterest) rtitles("`l'") se summstat() store(row`i') starlevels(10 5 1) starloc(1)
		outreg, replay(quasi1) append(row`i') ctitles("", I) store(quasi1) note("")
			local ++i
	}
	
	local i = 1
		foreach l of local min_levels {
		replace varinterest = `l'
		qui xi: reg zmss_22 varinterest zmss_21 $controls1 grade3 grade4 grade5 grade6 grade7 grade8 if grade36 == 1, cluster(gradeschoolunit)
		outreg, keep(varinterest) rtitles("`l'") se summstat() store(row`i') starlevels(10 5 1) starloc(1)
		outreg, replay(quasi2) append(row`i') ctitles("", II) store(quasi2) note("")
			local ++i
	}

	local i = 1
	*Table showing same info
	foreach l of local min_levels2 {
		replace varinterest = `l'
		qui xi: reg zmss_22 varinterest zmss_21 $controls1 grade3 grade4 grade5 grade6 grade7 grade8 if grade78 == 1, cluster(gradeschoolunit)
		outreg, keep(varinterest) rtitles("`l'") se summstat() store(row`i') starlevels(10 5 1) starloc(1)
		outreg, replay(quasi3) append(row`i') ctitles("", III) store(quasi3) note("")
			local ++i
	}
	
	mat bonus = J(24,1,.)
	
	forvalues i = 1(1)24 {
	    mat bonus[`i',1] = 0
	}

	frmttable, statmat(bonus) store(bonus) sfmt(g,f)
	outreg, replay(quasi3) append(bonus) rtitles("class_minutes1099" \ "" \ "R2"  \"N" \ "class_minutes1599" \ "" \ "R2" \ "N" \ "class_minutes2099" \ "" \ "R2" \ "N" \ "class_minutes2599" \ "" \ "R2" \ "N" \ "class_minutes3099" \ "" \ "R2" \ "N" \ "class_minutes3599" \ "" \ "R2" \ "N" \ "class_minutes4099" \ "" \ "R2" \ "N" \ "class_minutes4599" \ "" \ "R2" \ "N" \ "class_minutes5099" \ "" \ "R2" \ "N" \ "class_minutes5599" \ "" \ "R2" \ "N" \ "class_minutes6099" \ "" \ "R2" \ "N")

	outreg, replay(quasi1) merge(quasi2)
	outreg, replay(quasi1) merge(quasi3)

	outreg using "${outputpath}final_figures/quasi_graph_as_table.tex", ///
    replay(quasi1) tex fragment replace
	
	
	
	
	
	
	
*****************************************************************************
*****************************************************************************
********************************************************************************
*FIGURE: Same as above, with gradeschoolunit fixed affects
use temp, clear
drop if treatment == 0

local min_levels class_minutes1099 class_minutes1599 class_minutes2099 class_minutes2599 class_minutes3099 class_minutes3599 class_minutes4099 class_minutes4599 class_minutes5099 class_minutes5599 class_minutes6099
gen varinterest =.
local gradecuts allgrades grade36 //grade78
local i = 1
foreach g of local gradecuts {
    local m = 5
	foreach l of local min_levels {
		local m = `m' + 5
		replace varinterest = `l'
		xi: reg zmss_22 varinterest zmss_21 $controls1 if `g' == 1, cluster(gradeschoolunit) absorb(gradeschoolunit)
		estimates store m`m'
	}

	coefplot m10 || m15 || m20 || m25 || m30 || m35 || m40 || m45 || m50 || m55 || m60, drop(_cons zmss_21 age female white black hispanic native days_missed esl_ind speced freelunch) vertical bycoefs title("`: var label `g''") xtitle("At least X Minutes") name(graph`i', replace) yline(0) ylabel(,angle(horizontal) format(%3.1f)) xlab(1 "10" 2 "15" 3 "20" 4 "25" 5 "30" 6 "35" 7 "40" 8 "45" 9 "50" 10 "55" 11 "60")
	
	local ++i
}

local m = 5
foreach l of local min_levels {
	local m = `m' + 5
	replace varinterest = `l'
	label variable varinterest "`m'"
	qui xi: reg zmss_22 varinterest zmss_21 $controls1 if grade78 == 1, cluster(gradeschoolunit) absorb(gradeschoolunit)
	estimates store m`m'
}

coefplot m10 || m15 || m20 || m25 || m30, keep(varinterest) vertical bycoefs title("`: var label grade78'") xtitle("At least X Minutes") name(graph3, replace) yline(0) xlab(1 "10" 2 "15" 3 "20" 4 "25" 5 "30") ylabel(,angle(horizontal) format(%3.1f))



graph combine graph1 graph2 graph3, rows(1) cols(3) xsize(10) title("Effect of classroom with at least X practice minutes") subtitle("Weekly Class Average, w/ Grade-School FEs")

*graph export "${outputpath}final_figures/class_averages_effects_w_fe.png", replace









local gradecuts2 allgrades grade36
local min_levels2 class_minutes1099 class_minutes1599 class_minutes2099 class_minutes2599 class_minutes3099


	local i = 1
	foreach l of local min_levels {
		replace varinterest = `l'
		qui xi: reg zmss_22 varinterest zmss_21 $controls1, cluster(gradeschoolunit) absorb(gradeschoolunit)
		outreg, keep(varinterest) rtitles("`l'") se summstat() store(row`i') starlevels(10 5 1) starloc(1)
		outreg, replay(quasi1) append(row`i') ctitles("", I) store(quasi1) note("")
			local ++i
	}
	
	local i = 1
		foreach l of local min_levels {
		replace varinterest = `l'
		qui xi: reg zmss_22 varinterest zmss_21 $controls1 if grade36 == 1, cluster(gradeschoolunit) absorb(gradeschoolunit)
		outreg, keep(varinterest) rtitles("`l'") se summstat() store(row`i') starlevels(10 5 1) starloc(1)
		outreg, replay(quasi2) append(row`i') ctitles("", II) store(quasi2) note("")
			local ++i
	}

	local i = 1
	*Table showing same info
	foreach l of local min_levels2 {
		replace varinterest = `l'
		qui xi: reg zmss_22 varinterest zmss_21 $controls1 if grade78 == 1, cluster(gradeschoolunit) absorb(gradeschoolunit)
		outreg, keep(varinterest) rtitles("`l'") se summstat() store(row`i') starlevels(10 5 1) starloc(1)
		outreg, replay(quasi3) append(row`i') ctitles("", III) store(quasi3) note("")
			local ++i
	}
	
	mat bonus = J(24,1,.)
	
	forvalues i = 1(1)24 {
	    mat bonus[`i',1] = 0
	}

	frmttable, statmat(bonus) store(bonus) sfmt(g,f)
	outreg, replay(quasi3) append(bonus) rtitles("class_minutes1099" \ "" \ "R2"  \"N" \ "class_minutes1599" \ "" \ "R2" \ "N" \ "class_minutes2099" \ "" \ "R2" \ "N" \ "class_minutes2599" \ "" \ "R2" \ "N" \ "class_minutes3099" \ "" \ "R2" \ "N" \ "class_minutes3599" \ "" \ "R2" \ "N" \ "class_minutes4099" \ "" \ "R2" \ "N" \ "class_minutes4599" \ "" \ "R2" \ "N" \ "class_minutes5099" \ "" \ "R2" \ "N" \ "class_minutes5599" \ "" \ "R2" \ "N" \ "class_minutes6099" \ "" \ "R2" \ "N")

	outreg, replay(quasi1) merge(quasi2)
	outreg, replay(quasi1) merge(quasi3)

	outreg using "${outputpath}final_figures/quasi_graph_as_table_treatment_onlyfe.tex", ///
    replay(quasi1) tex fragment replace
	
	
*******************************************************************************
*******************************************************************************
*******************************************************************************
*TABLE: Quasi broken out by prior performance


use temp, clear
global controls "age female white black hispanic native days_missed esl_ind speced freelunch"

gen keen =.
replace keen = 1 if zmss_21 > .0802851
replace keen = 0 if zmss_21 < .0802851
replace keen =. if zmss_21 ==.

label variable keen ">50% 2021 STAAR"

gen slow =.
replace slow = 1 if zmss_21 < .0802851
replace slow = 0 if zmss_21 > .0802851
replace slow =. if zmss_21 ==.
label variable slow "<50% 2021 STAAR"

local gradecuts allgrades grade36
local cuts all keen slow
	
	
foreach c of local cuts {
	local i = 0
	foreach g of local gradecuts {
	    local ++i
		qui xi: reg zmss_22 class_minutes3599 zmss_21 $controls1 grade3 grade4 grade5 grade6 grade7 grade8 if `g' == 1 & `c' == 1, cluster(gradeschoolunit)
		outreg, keep(class_minutes3599) rtitle("`: var label `c''") se summstat(N) starlevels(10 5 1) starloc(1)
		outreg, replay(`c'`i'q) ctitles("", I) store(`c'`i'q)
		
		local ++i
		qui xi: reg zmss_22 class_minutes3599 zmss_21 $controls1 grade3 grade4 grade5 grade6 grade7 grade8 if `g' == 1 & `c' == 1, cluster(gradeschoolunit)  absorb(gradeschoolunit)
		outreg, keep(class_minutes3599) rtitle("`: var label `c''") se summstat(N) starlevels(10 5 1) starloc(1)
		outreg, replay(`c'`i'q) ctitle("", II) store(`c'`i'q)
	
	}

	forvalues i = 2(1)4 {
		outreg, replay(`c'1q) merge(`c'`i'q)
	}

		
	outreg, replay(`c'1q) nocenter note("") plain ctitles("", All Grades, "", Grades 3-6, "" \ "", I, II, I, II) multicol(1,2,2;1,4,2)
}		

local cuts2 keen slow
foreach c of local cuts2{
    	outreg, replay(all1q) append(`c'1q) ctitles("", All Grades, "", Grades 3-6, "" \ "", I, II, I, II) multicol(1,2,2;1,4,2) store(all1q)
}	



gen interaction = class_minutes3599 * keen

xi: reg zmss_22 class_minutes3599 interaction keen zmss_21 $controls1 grade3 grade4 grade5 grade6 grade7 grade8 if grade36 == 1, cluster(gradeschoolunit)
test class_minutes3599 + interaction = 0
	
	

	
	
	
	
	
********************************************************************************
preserve
drop if zmss_21 ==.
drop if grade36 == 0
global hetvars total_minutes classminutes class_minutes3599 msl_net classlu class_lu0399
label variable total_minutes "Individual Avg Weekly Minutes"
label variable classminutes "Classmate Avg Weekly Minutes"
label variable class_minutes3599 "Classmate Avg Weekly Minutes above 35"
label variable msl_net "Indiviudal Avg Weekly Level-ups"
label variable classlu "Classmate Avg Weekly Level-ups"
label variable class_lu0399 "Classmate Avg Weekly LU above 3"

* TABLE: Balance and Descriptive stats for student characteristics
local i = 1
foreach var in $hetvars {
    quietly xi: reg `var' keen
	outreg, keep(keen) rtitle("`: var label `var''") stats(b) ///
		noautosumm store(row`i') starlevels(10 5 1) starloc(1)
	outreg, replay(diffq) append(row`i') ctitles("",Difference) ///
		store(diffq) note("")
	local ++i
}
outreg, replay(diffq)
	
	
* Then Summary statistics
local count: word count $hetvars
mat sumstatq = J(`count',3,.)

local i = 1
foreach var in $hetvars {
    quietly: summarize `var'
	mat sumstatq[`i',1] = r(N)
	quietly: summarize `var' if keen ==0
	mat sumstatq[`i',2] = r(mean)

	quietly: summarize `var' if keen ==1
	mat sumstatq[`i',3] = r(mean)
	local i = `i' + 1
}
frmttable, statmat(sumstatq) store(sumstatq) sfmt(g,f,f)
	
outreg, replay(sumstatq) merge(diffq)
outreg, replay(sumstatq) nocenter note("") plain ctitles("", N, Slow, Keen, Diff) multicol(1,2,4)

outreg using "${outputpath}aisd_quasi_balance.tex", ///
    replay(sumstatq) tex fragment replace
	
	
restore









********************************************************************************
*FIGURE: Effect of minimum class level ups
use temp, clear
local min_levels class_lu0199 class_lu0299 class_lu0399 class_lu0499 class_lu0599
gen varinterest =.
local gradecuts allgrades grade36 //grade78

local i = 1
foreach g of local gradecuts {
    local m = 0
	foreach l of local min_levels {
		local m = `m' + 1
		replace varinterest = `l'
		qui xi: reg zmss_22 varinterest zmss_21 $controls1 grade3 grade4 grade5 grade6 grade7 grade8 if `g' == 1, cluster(gradeschoolunit)
		estimates store m`m'
	}

coefplot m1 || m2 || m3 || m4 || m5, drop(_cons zmss_21 age female white black hispanic native days_missed esl_ind speced freelunch grade3 grade4 grade5 grade6 grade7 grade8) vertical bycoefs title("`: var label `g''") xtitle("At least X Level-ups") name(graph`i', replace) yline(0) ylabel(,angle(horizontal) format(%3.1f)) xlab(1 "1" 2 "2" 3 "3" 4 "4" 5 "5")
	
	local ++i
}

    local m = 0
	foreach l of local min_levels {
		local m = `m' + 1
		replace varinterest = `l'
		qui xi: reg zmss_22 varinterest zmss_21 $controls1 grade3 grade4 grade5 grade6 grade7 grade8 if grade78 == 1, cluster(gradeschoolunit)
		estimates store m`m'
	}

coefplot m1 || m2, drop(_cons zmss_21 age female white black hispanic native days_missed esl_ind speced freelunch grade3 grade4 grade5 grade6 grade7 grade8) vertical bycoefs title("`: var label grade78'") xtitle("At least X Level-ups") name(graph3, replace) yline(0) ylabel(,angle(horizontal) format(%3.1f)) xlab(1 "1" 2 "2")



graph combine graph1 graph2 graph3, rows(1) cols(3) xsize(10) title("Effect of classroom with at least X Level-ups") subtitle("Weekly Class Average")

graph export "${outputpath}final_figures/class_averages_effectslu.png", replace


*Table of same graph
local gradecuts2 allgrades grade36
local min_levels2 class_lu0199 class_lu0299


	local i = 1
	foreach l of local min_levels {
		replace varinterest = `l'
		qui xi: reg zmss_22 varinterest zmss_21 $controls1 grade3 grade4 grade5 grade6 grade7 grade8, cluster(gradeschoolunit)
		outreg, keep(varinterest) rtitles("`l'") se summstat() store(row`i') starlevels(10 5 1) starloc(1)
		outreg, replay(quasi1b) append(row`i') ctitles("", I) store(quasi1b) note("")
			local ++i
	}
	
	local i = 1
		foreach l of local min_levels {
		replace varinterest = `l'
		qui xi: reg zmss_22 varinterest zmss_21 $controls1 grade3 grade4 grade5 grade6 grade7 grade8 if grade36 == 1, cluster(gradeschoolunit)
		outreg, keep(varinterest) rtitles("`l'") se summstat() store(row`i') starlevels(10 5 1) starloc(1)
		outreg, replay(quasi2b) append(row`i') ctitles("", II) store(quasi2b) note("")
			local ++i
	}

	local i = 1
	*Table showing same info
	foreach l of local min_levels2 {
		replace varinterest = `l'
		qui xi: reg zmss_22 varinterest zmss_21 $controls1 grade3 grade4 grade5 grade6 grade7 grade8 if grade78 == 1, cluster(gradeschoolunit)
		outreg, keep(varinterest) rtitles("`l'") se summstat() store(row`i') starlevels(10 5 1) starloc(1)
		outreg, replay(quasi3b) append(row`i') ctitles("", III) store(quasi3b) note("")
			local ++i
	}
	
	mat bonusb = J(12,1,.)
	
	forvalues i = 1(1)12 {
	    mat bonusb[`i',1] = 0
	}

	frmttable, statmat(bonusb) store(bonusb) sfmt(g,f)
	outreg, replay(quasi3b) append(bonusb) rtitles("class_lu0199" \ "" \ "R2"  \"N" \ "class_lu0299" \ "" \ "R2" \ "N" \ "class_lu0399" \ "" \ "R2" \ "N" \ "class_lu0499" \ "" \ "R2" \ "N" \ "class_lu0599" \ "" \ "R2" \ "N")

	outreg, replay(quasi1b) merge(quasi2b)
	outreg, replay(quasi1b) merge(quasi3b)

	outreg using "${outputpath}final_figures/quasi_lu_graph_as_table.tex", ///
    replay(quasi1b) tex fragment replace



	
	
	











********************************************************************************
*FIGURE: Same as above but with fixed effects
local i = 1
foreach g of local gradecuts {
    local m = 0
	foreach l of local min_levels {
		local m = `m' + 1
		replace varinterest = `l'
		qui xi: reg zmss_22 varinterest zmss_21 $controls1 grade3 grade4 grade5 grade6 grade7 grade8 if `g' == 1, cluster(gradeschoolunit) absorb(gradeschoolunit)
		estimates store m`m'
	}

coefplot m1 || m2 || m3 || m4 || m5, keep(varinterest) vertical bycoefs title("`: var label `g''") xtitle("At least X Minutes") name(graph`i', replace) yline(0)ylabel(,angle(horizontal) format(%3.1f)) xlab(1 "1" 2 "2" 3 "3" 4 "4" 5 "5")
	
	local ++i
}


local m = 5

    local m = 0
	foreach l of local min_levels {
		local m = `m' + 1
		replace varinterest = `l'
		qui xi: reg zmss_22 varinterest zmss_21 $controls1 grade3 grade4 grade5 grade6 grade7 grade8 if grade78 == 1, cluster(gradeschoolunit) absorb(gradeschoolunit)
		estimates store m`m'
	}

coefplot m1 || m2, drop(_cons zmss_21 age female white black hispanic native days_missed esl_ind speced freelunch grade3 grade4 grade5 grade6 grade7 grade8) vertical bycoefs title("`: var label grade78'") xtitle("At least X Level-ups") name(graph3, replace) ylabel(,angle(horizontal) format(%3.1f)) xlab(1 "1" 2 "2")









graph combine graph1 graph2 graph3, rows(1) cols(3) xsize(10) title("Effect of classroom with at least X level-ups") subtitle("Weekly Class Average, w/ Grade-School FEs")

graph export "${outputpath}final_figures/class_averages_effectslu_w_fe.png", replace







********************************************************************************
*TABLE: Simple OLS Coeff for practice time on a teacher's answers to presurvey

global PRESURVEY ka_purp_never ka_purp_demo ka_purp_optional ka_purp_required tech_access_stud multi_class_teach indep_work_in_school_0 indep_work_in_school_1_30 indep_work_in_school_31_60 indep_work_in_school_61_90 indep_work_in_school_90pl indep_work_after_school_0  indep_work_after_school_1_30 indep_work_after_school_31_60 indep_work_after_school_61_90 indep_work_after_school_90pl ka_use_in_school_0 ka_use_in_school_1le ka_use_in_school_1 ka_use_in_school_1pl ka_use_after_school_0 ka_use_after_school_1le ka_use_after_school_1 ka_use_after_school_1pl cat_use_in_school_0 cat_use_in_school_1le cat_use_in_school_1 cat_use_in_school_1pl cat_use_after_school_0 cat_use_after_school_1le cat_use_after_school_1 cat_use_after_school_1pl

use temp, clear

*retain variable labels
foreach v of var * {
	local l`v' : variable label `v'
	if `"`l`v''"' == "" {
		local l`v' "`v'"
	}
}

collapse (mean) ${PRESURVEY} obs* classminutes teachergrade grade3 grade4 grade5 grade6 grade7 grade8 gradeschoolunit treatment, by(teacher_id)

*Reattach labels
foreach v of var * {
	label var `v' `"`l`v''"'
}

gen class_minutes1099 = classminutes>=10
gen class_minutes1599 = classminutes>=15
gen class_minutes2099 = classminutes>=20
gen class_minutes2599 = classminutes>=25
gen class_minutes3099 = classminutes>=30
gen class_minutes3599 = classminutes>=35
gen class_minutes4099 = classminutes>=40
gen class_minutes4599 = classminutes>=45
gen class_minutes5099 = classminutes>=50
gen class_minutes5599 = classminutes>=55
gen class_minutes6099 = classminutes>=65

drop if treatment == 0

xi: reg classminutes ${PRESURVEY} i.teachergrade, cluster(gradeschoolunit)

replace indep_work_in_school_1_30 = 1 if indep_work_in_school_31_60 == 1 | indep_work_in_school_61_90 == 1 | indep_work_in_school_90pl == 1
replace indep_work_in_school_31_60 = 1 if indep_work_in_school_61_90 == 1 | indep_work_in_school_90pl == 1
replace indep_work_in_school_61_90 = 1 if indep_work_in_school_90pl == 1

replace indep_work_after_school_1_30 = 1 if indep_work_after_school_31_60 == 1 | indep_work_after_school_61_90 == 1 | indep_work_after_school_90pl == 1
replace indep_work_after_school_31_60 = 1 if indep_work_after_school_61_90 == 1 | indep_work_after_school_90pl == 1
replace indep_work_after_school_61_90 = 1 if indep_work_after_school_90pl == 1



*Used khan before (purpose)
quietly xi: reg classminutes ka_purp_demo ka_purp_optional ka_purp_required i.teachergrade, cluster(gradeschoolunit) 
outreg, keep(ka_purp_demo ka_purp_optional ka_purp_required) se noautosumm store(row1) starlevels(10 5 1) starloc(1)
outreg, replay(correl) append(row1) ctitles("", Coeff.) store(correl) note("")

*Student Tech Access
quietly xi: reg classminutes tech_access_stud i.teachergrade, cluster(gradeschoolunit) 
outreg, keep(tech_access_stud) se noautosumm store(row2) starlevels(10 5 1) starloc(1)
outreg, replay(correl) append(row2) ctitles("", Coeff.) store(correl) note("")

*Teaches Multiple classrooms
quietly xi: reg classminutes multi_class_teach i.teachergrade, cluster(gradeschoolunit) 
outreg, keep(multi_class_teach) se noautosumm store(row3) starlevels(10 5 1) starloc(1)
outreg, replay(correl) append(row3) ctitles("", Coeff.) store(correl) note("")

*Independent work in school
quietly xi: reg classminutes indep_work_in_school_31_60 indep_work_in_school_61_90 indep_work_in_school_90pl i.teachergrade, cluster(gradeschoolunit) 
outreg, keep(indep_work_in_school_31_60 indep_work_in_school_61_90 indep_work_in_school_90pl) se noautosumm store(row4) starlevels(10 5 1) starloc(1)
outreg, replay(correl) append(row4) ctitles("", Coeff.) store(correl) note("")

*Independent work after school
quietly xi: reg classminutes indep_work_after_school_1_30 indep_work_after_school_31_60 indep_work_after_school_61_90 indep_work_after_school_90pl i.teachergrade, cluster(gradeschoolunit) 
outreg, keep(indep_work_after_school_1_30 indep_work_after_school_31_60 indep_work_after_school_61_90 indep_work_after_school_90pl) se noautosumm store(row5) starlevels(10 5 1) starloc(1)
outreg, replay(correl) append(row5) ctitles("", Coeff.) store(correl) note("")

*Khan use in school
quietly xi: reg classminutes ka_use_in_school_1le ka_use_in_school_1 ka_use_in_school_1pl i.teachergrade, cluster(gradeschoolunit) 
outreg, keep(ka_use_in_school_1le ka_use_in_school_1 ka_use_in_school_1pl) se noautosumm store(row6) starlevels(10 5 1) starloc(1)
outreg, replay(correl) append(row6) ctitles("", Coeff.) store(correl) note("")

*Khan use after school
quietly xi: reg classminutes ka_use_after_school_1le ka_use_after_school_1 ka_use_after_school_1pl i.teachergrade, cluster(gradeschoolunit) 
outreg, keep(ka_use_after_school_1le ka_use_after_school_1 ka_use_after_school_1pl) se noautosumm store(row7) starlevels(10 5 1) starloc(1)
outreg, replay(correl) append(row7) ctitles("", Coeff.) store(correl) note("")

*CAL (non Khan) use in school
quietly xi: reg classminutes cat_use_in_school_1le cat_use_in_school_1 cat_use_in_school_1pl i.teachergrade, cluster(gradeschoolunit) 
outreg, keep(cat_use_in_school_1le cat_use_in_school_1 cat_use_in_school_1pl) se noautosumm store(row8) starlevels(10 5 1) starloc(1)
outreg, replay(correl) append(row8) ctitles("", Coeff.) store(correl) note("")

*CAL (non Khan) use after school
quietly xi: reg classminutes cat_use_after_school_1le cat_use_after_school_1 cat_use_after_school_1pl i.teachergrade, cluster(gradeschoolunit) 
outreg, keep(cat_use_after_school_1le cat_use_after_school_1 cat_use_after_school_1pl) se noautosumm store(row9) starlevels(10 5 1) starloc(1)
outreg, replay(correl) append(row9) ctitles("", Coeff.) store(correl) note("")


outreg using "${outputpath}final_figures/practice_correl1.tex", ///
    replay(correl) tex fragment replace
	
	
	
	
	
********************************************************************************
*TABLE: Same as above, but instead of average class minutes we use whether their class averages +25 minutes
*Correlation table v2, class_minutes25+

*Used khan before (purpose)
quietly xi: reg class_minutes2599 ka_purp_demo ka_purp_optional ka_purp_required i.teachergrade, cluster(gradeschoolunit) 
outreg, keep(ka_purp_demo ka_purp_optional ka_purp_required) se noautosumm store(row1) starlevels(10 5 1) starloc(1)
outreg, replay(corre2) append(row1) ctitles("", Coeff.) store(corre2) note("")

*Student Tech Access
quietly xi: reg class_minutes2599 tech_access_stud i.teachergrade, cluster(gradeschoolunit) 
outreg, keep(tech_access_stud) se noautosumm store(row2) starlevels(10 5 1) starloc(1)
outreg, replay(corre2) append(row2) ctitles("", Coeff.) store(corre2) note("")

*Teaches Multiple classrooms
quietly xi: reg class_minutes2599 multi_class_teach i.teachergrade, cluster(gradeschoolunit) 
outreg, keep(multi_class_teach) se noautosumm store(row3) starlevels(10 5 1) starloc(1)
outreg, replay(corre2) append(row3) ctitles("", Coeff.) store(corre2) note("")

*Independent work in school
quietly xi: reg class_minutes2599 indep_work_in_school_31_60 indep_work_in_school_61_90 indep_work_in_school_90pl i.teachergrade, cluster(gradeschoolunit) 
outreg, keep(indep_work_in_school_31_60 indep_work_in_school_61_90 indep_work_in_school_90pl) se noautosumm store(row4) starlevels(10 5 1) starloc(1)
outreg, replay(corre2) append(row4) ctitles("", Coeff.) store(corre2) note("")

*Independent work after school
quietly xi: reg class_minutes2599 indep_work_after_school_1_30 indep_work_after_school_31_60 indep_work_after_school_61_90 indep_work_after_school_90pl i.teachergrade, cluster(gradeschoolunit) 
outreg, keep(indep_work_after_school_1_30 indep_work_after_school_31_60 indep_work_after_school_61_90 indep_work_after_school_90pl) se noautosumm store(row5) starlevels(10 5 1) starloc(1)
outreg, replay(corre2) append(row5) ctitles("", Coeff.) store(corre2) note("")

*Khan use in school
quietly xi: reg class_minutes2599 ka_use_in_school_1le ka_use_in_school_1 ka_use_in_school_1pl i.teachergrade, cluster(gradeschoolunit) 
outreg, keep(ka_use_in_school_1le ka_use_in_school_1 ka_use_in_school_1pl) se noautosumm store(row6) starlevels(10 5 1) starloc(1)
outreg, replay(corre2) append(row6) ctitles("", Coeff.) store(corre2) note("")

*Khan use after school
quietly xi: reg class_minutes2599 ka_use_after_school_1le ka_use_after_school_1 ka_use_after_school_1pl i.teachergrade, cluster(gradeschoolunit) 
outreg, keep(ka_use_after_school_1le ka_use_after_school_1 ka_use_after_school_1pl) se noautosumm store(row7) starlevels(10 5 1) starloc(1)
outreg, replay(corre2) append(row7) ctitles("", Coeff.) store(corre2) note("")

*CAL (non Khan) use in school
quietly xi: reg class_minutes2599 cat_use_in_school_1le cat_use_in_school_1 cat_use_in_school_1pl i.teachergrade, cluster(gradeschoolunit) 
outreg, keep(cat_use_in_school_1le cat_use_in_school_1 cat_use_in_school_1pl) se noautosumm store(row8) starlevels(10 5 1) starloc(1)
outreg, replay(corre2) append(row8) ctitles("", Coeff.) store(corre2) note("")

*CAL (non Khan) use after school
quietly xi: reg class_minutes2599 cat_use_after_school_1le cat_use_after_school_1 cat_use_after_school_1pl i.teachergrade, cluster(gradeschoolunit) 
outreg, keep(cat_use_after_school_1le cat_use_after_school_1 cat_use_after_school_1pl) se noautosumm store(row9) starlevels(10 5 1) starloc(1)
outreg, replay(corre2) append(row9) ctitles("", Coeff.) store(corre2) note("")


outreg using "${outputpath}final_figures/practice_correl2.tex", ///
    replay(corre2) tex fragment replace

	
	


	
********************************************************************************
*TABLE: Summary statistics for treatment onboarding survey answers	
use temp, clear

*retain variable labels
foreach v of var * {
	local l`v' : variable label `v'
	if `"`l`v''"' == "" {
		local l`v' "`v'"
	}
}

collapse (mean) obs* classminutes teachergrade grade3 grade4 grade5 grade6 grade7 grade8 gradeschoolunit treatment grade36 grade78, by(teacher_id)

*Reattach labels
foreach v of var * {
	label var `v' `"`l`v''"'
}

drop if treatment == 0

	local obslist obs_classes_per_week obs_mins_per_class obs_during_1wk_30le obs_during_1wk_30pl obs_during_1pl_30le obs_during_1pl_30pl obs_after_easy obs_after_hw obs_after_no_hw obs_after_hw_unf obs_after_hw_unc obs_after_hw_graded obs_after_no_tech obs_after_lunch obs_strat_1 obs_strat_twice_30 obs_strat_each_day obs_strat_once_60 obs_strat_try_more obs_parents_email obs_parents_text obs_parents_handout obs_parents_remind obs_parents_busy obs_parents_no_hw
	
	
		local duringlist obs_classes_per_week obs_mins_per_class obs_during_1wk_30le obs_during_1wk_30pl obs_during_1pl_30le obs_during_1pl_30pl obs_after_easy obs_after_hw obs_after_no_hw obs_after_hw_unf obs_after_hw_unc obs_after_hw_graded obs_after_no_tech obs_after_lunch
		
		
			local stratlist obs_strat_1 obs_strat_twice_30 obs_strat_each_day obs_strat_once_60 obs_strat_try_more obs_parents_email obs_parents_text obs_parents_handout obs_parents_remind obs_parents_busy obs_parents_no_hw
	
	foreach var of local obslist {
	    qui summ `var'
		local mean_`var' = string(`r(mean)',"%9.2f")
		qui summ `var' if teachergrade <= 6
		local mean_36_`var' = string(`r(mean)',"%9.2f")
		qui summ `var' if teachergrade > 6
		local mean_78_`var' = string(`r(mean)',"%9.2f")
		
	}
	

	
	local list1 obs_classes_per_week obs_mins_per_class
	local list2 obs_during_1wk_30le obs_during_1wk_30pl obs_during_1pl_30le obs_during_1pl_30pl
	local list3 obs_after_easy obs_after_no_hw obs_after_hw_unf obs_after_hw obs_after_hw_unc obs_after_hw_graded obs_after_no_tech obs_after_lunch
	local list4 obs_strat_1 obs_strat_once_60 obs_strat_twice_30 obs_strat_each_day obs_strat_try_more
	local list5 obs_parents_email obs_parents_text obs_parents_handout obs_parents_remind obs_parents_busy obs_parents_no_hw	
	
	
	
capture file close outfile
	file open outfile using "${outputpath}obs_sum.tex", write replace
		file write outfile "\begin{tabular}{lccc}" _n
		** Create Headings
		file write outfile " & All Treatment & Grade 3-6 & Grade 7-8 \\ \hline \hline" _n
		** Fill in the table 
		foreach var of local list1 { 
			file write outfile " `: var label `var'' & `mean_`var'' & `mean_36_`var'' &  `mean_78_`var'' \\ "  _n
		}
		file write outfile "\multicolumn{3}{l}{\textbf{How easy do you expect it would be for you to provide some time each week to work on Khan Academy DURING CLASS?}} \\ \hline" _n
		foreach var of local list2 { 
			file write outfile " `: var label `var'' & `mean_`var'' & `mean_36_`var'' &  `mean_78_`var'' \\ "  _n
		}
		file write outfile "\multicolumn{3}{l}{\textbf{How easy do you expect it would be for students to practice on Khan Academy AFTER SCHOOL for at least an hour a week?}} \\ \hline" _n
		foreach var of local list3 { 
			file write outfile " `: var label `var'' & `mean_`var'' & `mean_36_`var'' &  `mean_78_`var'' \\ "  _n
		}
		file write outfile "\multicolumn{3}{l}{\textbf{Which homework guideline do you think would work for your students (and their parents) to try to follow?}} \\ \hline" _n
		foreach var of local list4 { 
			file write outfile " `: var label `var'' & `mean_`var'' & `mean_36_`var'' &  `mean_78_`var'' \\ "  _n
		}
		file write outfile "\multicolumn{3}{l}{\textbf{How easy do you expect it would be to communicate with parents about the program, asking for help to facilitate at least an hour of practice at home?}} \\ \hline" _n	
		foreach var of local list5 { 
			file write outfile " `: var label `var'' & `mean_`var'' & `mean_36_`var'' &  `mean_78_`var'' \\ "  _n
		}		
		file write outfile " N & 105 & 80 & 25 \\ " _n
		file write outfile "\hline & & & \\ \hline \hline \end{tabular}"
	file close outfile
	
	
	

	

********************************************************************************
*TABLE: Correlation between Onboarding survey answers and practice time
mmerge teacher_id using "${datapath}teacher_meetings.dta"
drop if _merge == 2
drop _merge
gen class_minutes3599 = classminutes>=35

local gradecut grade36 grade78
foreach g of local gradecut {   
preserve
drop if `g' == 0


lab var attended_pd "Teacher attended PD session during district PD Day"
lab var first_meeting "Teacher held first meeting with khollaborator"
lab var followup "Teacher held followup meeting with khollaborator prior to Oct 1st, 2021"
local corrlist attended_pd first_meeting followup obs_classes_per_week obs_mins_per_class obs_during_1wk_30le obs_during_1wk_30pl obs_during_1pl_30le obs_during_1pl_30pl obs_after_easy obs_after_hw obs_after_no_hw obs_after_hw_unf obs_after_hw_unc obs_after_hw_graded obs_after_no_tech obs_after_lunch obs_strat_1 obs_strat_twice_30 obs_strat_each_day obs_strat_once_60 obs_strat_try_more obs_parents_email obs_parents_text obs_parents_handout obs_parents_remind obs_parents_busy obs_parents_no_hw
	
	foreach var of local corrlist {
	    qui xi: reg class_minutes3599 `var' i.teachergrade, cluster(gradeschoolunit)
		
		local t = _b[`var']/_se[`var']
		local p =2*ttail(e(df_r),abs(`t'))
   
	** Manually  Add in Stars Based on significant levels 
		loc star "" 
		if `p' < 0.01 {
		loc star "***"
		}
		if `p' <0.05 & `p' > 0.01 { 
		loc star "**"
		}
		if `p' < 0.1 & `p' > 0.05 {
		loc star "*"
		}
		local `var'_b = string(_b[`var'],"%9.2f") + "`star'"
		local `var'_se = "(" + string(_se[`var'], "%9.2f") + ")"
	}
	
	local list1 attended_pd first_meeting followup obs_classes_per_week obs_mins_per_class
	local list2 obs_during_1wk_30le obs_during_1wk_30pl obs_during_1pl_30le obs_during_1pl_30pl
	local list3 obs_after_easy obs_after_no_hw obs_after_hw_unf obs_after_hw obs_after_hw_unc obs_after_hw_graded obs_after_no_tech obs_after_lunch
	local list4 obs_strat_1 obs_strat_once_60 obs_strat_twice_30 obs_strat_each_day obs_strat_try_more
	local list5 obs_parents_email obs_parents_text obs_parents_handout obs_parents_remind obs_parents_busy obs_parents_no_hw	
	
	capture file close outfile
	file open outfile using "${outputpath}minutes_corr_`g'.tex", write replace
		file write outfile "\begin{tabular}{lcc}" _n
		** Create Headings
		file write outfile " & Coeff & SE \\ \hline" _n
		** Fill in the table 
		foreach var of local list1 { 
			file write outfile "`: var label `var'' & ``var'_b' & ``var'_se' \\ "  _n
		}
		file write outfile "\multicolumn{3}{l}{\textbf{How easy do you expect it would be for you to provide some time each week to work on Khan Academy DURING CLASS?}} \\ \hline" _n
		foreach var of local list2 { 
			file write outfile "`: var label `var'' & ``var'_b' & ``var'_se' \\ "  _n
		}
		file write outfile "\multicolumn{3}{l}{\textbf{How easy do you expect it would be for students to practice on Khan Academy AFTER SCHOOL for at least an hour a week?}} \\ \hline" _n
		foreach var of local list3 { 
			file write outfile "`: var label `var'' & ``var'_b' & ``var'_se' \\ "  _n
		}
		file write outfile "\multicolumn{3}{l}{\textbf{Which homework guideline do you think would work for your students (and their parents) to try to follow?}} \\ \hline" _n
		foreach var of local list4 { 
			file write outfile "`: var label `var'' & ``var'_b' & ``var'_se' \\ "  _n
		}
		file write outfile "\multicolumn{3}{l}{\textbf{How easy do you expect it would be to communicate with parents about the program, asking for help to facilitate at least an hour of practice at home?}} \\ \hline" _n	
		foreach var of local list5 { 
			file write outfile "`: var label `var'' & ``var'_b' & ``var'_se' \\ "  _n
		}
		
		
		
		
		file write outfile "\hline  \end{tabular}"
	file close outfile
	

	
restore
}
	
	
	
**** Pre survey Correlation with practice minutes
use temp, clear

*retain variable labels
foreach v of var * {
	local l`v' : variable label `v'
	if `"`l`v''"' == "" {
		local l`v' "`v'"
	}
}

collapse (mean) ${PRESURVEY} obs* classminutes teachergrade grade3 grade4 grade5 grade6 grade7 grade8 gradeschoolunit treatment grade36 grade78, by(teacher_id)

*Reattach labels
foreach v of var * {
	label var `v' `"`l`v''"'
}

drop if treatment == 0
gen low_tech_access = 0
replace low_tech_access = 1 if tech_access < 75



rename indep_work_after_school_1_30 indep_work_after_school_1
rename indep_work_after_school_31_60 indep_work_after_school_31
rename indep_work_after_school_61_90 indep_work_after_school_61
rename indep_work_after_school_90pl indep_work_after_school_90


local corrlist low_tech_access tech_access_stud multi_class_teach ka_purp_never ka_purp_demo ka_purp_optional ka_purp_required indep_work_in_school_0 indep_work_in_school_1_30 indep_work_in_school_31_60 indep_work_in_school_61_90 indep_work_in_school_90pl indep_work_after_school_0  indep_work_after_school_1 indep_work_after_school_31 indep_work_after_school_61 indep_work_after_school_90 ka_use_in_school_0 ka_use_in_school_1le ka_use_in_school_1 ka_use_in_school_1pl ka_use_after_school_0 ka_use_after_school_1le ka_use_after_school_1 ka_use_after_school_1pl cat_use_in_school_0 cat_use_in_school_1le cat_use_in_school_1 cat_use_in_school_1pl cat_use_after_school_0 cat_use_after_school_1le cat_use_after_school_1 cat_use_after_school_1pl

local list1 low_tech_access tech_access_stud multi_class_teach
local list2 ka_purp_never ka_purp_demo ka_purp_optional ka_purp_required
local list3 indep_work_in_school_0 indep_work_in_school_1_30 indep_work_in_school_31_60 indep_work_in_school_61_90 indep_work_in_school_90pl
local list4 indep_work_after_school_0  indep_work_after_school_1 indep_work_after_school_31 indep_work_after_school_61 indep_work_after_school_90
local list5 ka_use_in_school_0 ka_use_in_school_1le ka_use_in_school_1 ka_use_in_school_1pl
local list6 ka_use_after_school_0 ka_use_after_school_1le ka_use_after_school_1 ka_use_after_school_1pl
local list7 cat_use_in_school_0 cat_use_in_school_1le cat_use_in_school_1 cat_use_in_school_1pl
local list8 cat_use_after_school_0 cat_use_after_school_1le cat_use_after_school_1 cat_use_after_school_1pl

gen class_minutes3599 = classminutes>=35

local gradecut grade36 grade78
foreach g of local gradecut {
    preserve
	drop if `g' == 0
	
	foreach var of local corrlist {
	    qui xi: reg class_minutes3599 `var' i.teachergrade, cluster(gradeschoolunit)
		
		local t = _b[`var']/_se[`var']
		local p =2*ttail(e(df_r),abs(`t'))
   
	** Manually  Add in Stars Based on significant levels 
		loc star "" 
		if `p' < 0.01 {
		loc star "***"
		}
		if `p' <0.05 & `p' > 0.01 { 
		loc star "**"
		}
		if `p' < 0.1 & `p' > 0.05 {
		loc star "*"
		}
		local `var'_b = string(_b[`var'],"%9.2f") + "`star'"
		local `var'_se = "(" + string(_se[`var'], "%9.2f") + ")"
	}
	
	
	capture file close outfile
	file open outfile using "${outputpath}minutes_ps_corr_`g'.tex", write replace
		file write outfile "\begin{tabular}{lcc}" _n
		** Create Headings
		file write outfile " & Coeff & SE \\ \hline" _n
		** Fill in the table 
		foreach var of local list1 { 
			file write outfile "`: var label `var'' & ``var'_b' & ``var'_se' \\ "  _n
		}
		file write outfile "\multicolumn{3}{l}{\textbf{Prior Khan Use}} \\ \hline" _n
		foreach var of local list2 { 
			file write outfile "`: var label `var'' & ``var'_b' & ``var'_se' \\ "  _n
		}
			file write outfile "\multicolumn{3}{l}{\textbf{Student Independent Work in School}} \\ \hline" _n
		foreach var of local list3 { 
			file write outfile "`: var label `var'' & ``var'_b' & ``var'_se' \\ "  _n
		}
			file write outfile "\multicolumn{3}{l}{\textbf{Student Independent Work after School}} \\ \hline" _n
		foreach var of local list4 { 
			file write outfile "`: var label `var'' & ``var'_b' & ``var'_se' \\ "  _n
		}
			file write outfile "\multicolumn{3}{l}{\textbf{Khan Academy Use in School}} \\ \hline" _n
		foreach var of local list5 { 
			file write outfile "`: var label `var'' & ``var'_b' & ``var'_se' \\ "  _n
		}
			file write outfile "\multicolumn{3}{l}{\textbf{Khan Academy Use after School}} \\ \hline" _n
		foreach var of local list6 { 
			file write outfile "`: var label `var'' & ``var'_b' & ``var'_se' \\ "  _n
		}
			file write outfile "\multicolumn{3}{l}{\textbf{Non-Khan CAL Use in School}} \\ \hline" _n
		foreach var of local list7 { 
			file write outfile "`: var label `var'' & ``var'_b' & ``var'_se' \\ "  _n
		}
			file write outfile "\multicolumn{3}{l}{\textbf{Non-Khan CAL Use after School}} \\ \hline" _n
		foreach var of local list8 { 
			file write outfile "`: var label `var'' & ``var'_b' & ``var'_se' \\ "  _n
		}
		
		
		file write outfile "\hline  \end{tabular}"
	file close outfile
	
	restore
}
	
	
	
* Pre randomization descriptive
use temp, clear
foreach v of var * {
	local l`v' : variable label `v'
	if `"`l`v''"' == "" {
		local l`v' "`v'"
	}
}


collapse (mean) ${PRESURVEY} classminutes teachergrade gradeschoolunit treatment, by(teacher_id)


*Reattach labels
foreach v of var * {
	label var `v' `"`l`v''"'
}

mmerge teacher_id using "${datapath}kholl_teach.dta"
drop if _merge == 2
drop _merge
gen class_minutes1099 = classminutes>=10
gen class_minutes1599 = classminutes>=15
gen class_minutes2099 = classminutes>=20
gen class_minutes2599 = classminutes>=25
gen class_minutes3099 = classminutes>=30
gen class_minutes3599 = classminutes>=35
gen class_minutes4099 = classminutes>=40
gen class_minutes4599 = classminutes>=45
gen class_minutes5099 = classminutes>=50
gen class_minutes5599 = classminutes>=55
gen class_minutes6099 = classminutes>=65

gen grade3= teachergrade==3
gen grade4= teachergrade==4
gen grade5= teachergrade==5
gen grade6= teachergrade==6
gen grade7= teachergrade==7
gen grade8 = teachergrade==8

rename indep_work_after_school_1_30 indep_work_after_school_1
rename indep_work_after_school_31_60 indep_work_after_school_31
rename indep_work_after_school_61_90 indep_work_after_school_61
rename indep_work_after_school_90pl indep_work_after_school_90


local corrlist ka_purp_never ka_purp_demo ka_purp_optional ka_purp_required tech_access_stud multi_class_teach indep_work_in_school_0 indep_work_in_school_1_30 indep_work_in_school_31_60 indep_work_in_school_61_90 indep_work_in_school_90pl indep_work_after_school_0  indep_work_after_school_1 indep_work_after_school_31 indep_work_after_school_61 indep_work_after_school_90 ka_use_in_school_0 ka_use_in_school_1le ka_use_in_school_1 ka_use_in_school_1pl ka_use_after_school_0 ka_use_after_school_1le ka_use_after_school_1 ka_use_after_school_1pl cat_use_in_school_0 cat_use_in_school_1le cat_use_in_school_1 cat_use_in_school_1pl cat_use_after_school_0 cat_use_after_school_1le cat_use_after_school_1 cat_use_after_school_1pl

	foreach var of local corrlist {
	    qui summ `var'
		local m_`var' = string(`r(mean)',"%9.2f")
		local n_`var' = r(N)
		qui summ `var' if teachergrade <= 6
		local m_36_`var' = string(`r(mean)',"%9.2f")
		local n_36_`var' = r(N)
		qui summ `var' if teachergrade > 6
		local m_78_`var' = string(`r(mean)',"%9.2f")
		local n_78_`var' = r(N)
		
	}
	

	
	
capture file close outfile
	file open outfile using "${outputpath}prs_desc.tex", write replace
		file write outfile "\begin{tabular}{lccc}" _n
		** Create Headings
		file write outfile " & All Treatment & Grade 3-6 & Grade 7-8 \\ \hline \hline" _n
		file write outfile " & & &\\"
		** Fill in the table 
		foreach var of local corrlist { 
			file write outfile " `: var label `var'' & `m_`var'' & `m_36_`var'' &  `m_78_`var'' \\ "  _n
		}
		file write outfile " N & `n_ka_purp_never' & `n_36_ka_purp_never' & `n_78_ka_purp_never' \\ " _n
		file write outfile "\hline & & & \\ \hline \hline \end{tabular}"
	file close outfile	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
* What students are practicing more?

use temp, clear	




*Used khan before (purpose)
quietly xi: reg total_minutes zmss_21 i.teachergrade, cluster(gradeschoolunit) 
outreg, keep(zmss_21) se summstat(N) store(row1) starlevels(10 5 1) starloc(1)
outreg, replay(tablex) append(row1) ctitles("", Coeff.) store(tablex) note("")

quietly xi: reg total_minutes female i.teachergrade, cluster(gradeschoolunit) 
outreg, keep(female) se summstat(N) store(row2) starlevels(10 5 1) starloc(1)
outreg, replay(tablex) append(row2) ctitles("", Coeff.) store(tablex) note("")

quietly xi: reg total_minutes white i.teachergrade, cluster(gradeschoolunit) 
outreg, keep(white) se summstat(N) store(row3) starlevels(10 5 1) starloc(1)
outreg, replay(tablex) append(row3) ctitles("", Coeff.) store(tablex) note("")

quietly xi: reg total_minutes black i.teachergrade, cluster(gradeschoolunit) 
outreg, keep(black) se summstat(N) store(row4) starlevels(10 5 1) starloc(1)
outreg, replay(tablex) append(row4) ctitles("", Coeff.) store(tablex) note("")

quietly xi: reg total_minutes native i.teachergrade, cluster(gradeschoolunit) 
outreg, keep(native) se summstat(N) store(row5) starlevels(10 5 1) starloc(1)
outreg, replay(tablex) append(row5) ctitles("", Coeff.) store(tablex) note("")

quietly xi: reg total_minutes hispanic i.teachergrade, cluster(gradeschoolunit) 
outreg, keep(hispanic) se summstat(N) store(row6) starlevels(10 5 1) starloc(1)
outreg, replay(tablex) append(row6) ctitles("", Coeff.) store(tablex) note("")

quietly xi: reg total_minutes freelunch i.teachergrade, cluster(gradeschoolunit) 
outreg, keep(freelunch) se summstat(N) store(row7) starlevels(10 5 1) starloc(1)
outreg, replay(tablex) append(row7) ctitles("", Coeff.) store(tablex) note("")

quietly xi: reg total_minutes speced i.teachergrade, cluster(gradeschoolunit) 
outreg, keep(speced) se summstat(N) store(row8) starlevels(10 5 1) starloc(1)
outreg, replay(tablex) append(row8) ctitles("", Coeff.) store(tablex) note("")








*Used khan before (purpose)
quietly xi: reg total_minutes zmss_21 i.teachergrade if classminutes > 25, cluster(gradeschoolunit) 
outreg, keep(zmss_21) se summstat(N) store(row1) starlevels(10 5 1) starloc(1)
outreg, replay(tabley) append(row1) ctitles("", Coeff.) store(tabley) note("")

quietly xi: reg total_minutes female i.teachergrade if classminutes > 25, cluster(gradeschoolunit) 
outreg, keep(female) se summstat(N) store(row2) starlevels(10 5 1) starloc(1)
outreg, replay(tabley) append(row2) ctitles("", Coeff.) store(tabley) note("")

quietly xi: reg total_minutes white i.teachergrade if classminutes > 25, cluster(gradeschoolunit) 
outreg, keep(white) se summstat(N) store(row3) starlevels(10 5 1) starloc(1)
outreg, replay(tabley) append(row3) ctitles("", Coeff.) store(tabley) note("")

quietly xi: reg total_minutes black i.teachergrade if classminutes > 25, cluster(gradeschoolunit) 
outreg, keep(black) se summstat(N) store(row4) starlevels(10 5 1) starloc(1)
outreg, replay(tabley) append(row4) ctitles("", Coeff.) store(tabley) note("")

quietly xi: reg total_minutes native i.teachergrade if classminutes > 25, cluster(gradeschoolunit) 
outreg, keep(native) se summstat(N) store(row5) starlevels(10 5 1) starloc(1)
outreg, replay(tabley) append(row5) ctitles("", Coeff.) store(tabley) note("")

quietly xi: reg total_minutes hispanic i.teachergrade if classminutes > 25, cluster(gradeschoolunit) 
outreg, keep(hispanic) se summstat(N) store(row6) starlevels(10 5 1) starloc(1)
outreg, replay(tabley) append(row6) ctitles("", Coeff.) store(tabley) note("")

quietly xi: reg total_minutes freelunch i.teachergrade if classminutes > 25, cluster(gradeschoolunit) 
outreg, keep(freelunch) se summstat(N) store(row7) starlevels(10 5 1) starloc(1)
outreg, replay(tabley) append(row7) ctitles("", Coeff.) store(tabley) note("")

quietly xi: reg total_minutes speced i.teachergrade if classminutes > 25, cluster(gradeschoolunit) 
outreg, keep(speced) se summstat(N) store(row8) starlevels(10 5 1) starloc(1)
outreg, replay(tabley) append(row8) ctitles("", Coeff.) store(tabley) note("")

outreg, replay(tablex) merge(tabley)

outreg using "${outputpath}final_figures/practice_by_stud_char.tex", ///
    replay(tablex) tex fragment replace
	
	
	
	
	
	
	
	
* What students are leveling-up more?

use temp, clear	

*Used khan before (purpose)
quietly xi: reg msl_net zmss_21 i.teachergrade, cluster(gradeschoolunit) 
outreg, keep(zmss_21) se summstat(N) store(row1) starlevels(10 5 1) starloc(1)
outreg, replay(tablez) append(row1) ctitles("", Coeff.) store(tablez) note("")

quietly xi: reg msl_net female i.teachergrade, cluster(gradeschoolunit) 
outreg, keep(female) se summstat(N) store(row2) starlevels(10 5 1) starloc(1)
outreg, replay(tablez) append(row2) ctitles("", Coeff.) store(tablez) note("")

quietly xi: reg msl_net white i.teachergrade, cluster(gradeschoolunit) 
outreg, keep(white) se summstat(N) store(row3) starlevels(10 5 1) starloc(1)
outreg, replay(tablez) append(row3) ctitles("", Coeff.) store(tablez) note("")

quietly xi: reg msl_net black i.teachergrade, cluster(gradeschoolunit) 
outreg, keep(black) se summstat(N) store(row4) starlevels(10 5 1) starloc(1)
outreg, replay(tablez) append(row4) ctitles("", Coeff.) store(tablez) note("")

quietly xi: reg msl_net native i.teachergrade, cluster(gradeschoolunit) 
outreg, keep(native) se summstat(N) store(row5) starlevels(10 5 1) starloc(1)
outreg, replay(tablez) append(row5) ctitles("", Coeff.) store(tablez) note("")

quietly xi: reg msl_net hispanic i.teachergrade, cluster(gradeschoolunit) 
outreg, keep(hispanic) se summstat(N) store(row6) starlevels(10 5 1) starloc(1)
outreg, replay(tablez) append(row6) ctitles("", Coeff.) store(tablez) note("")

quietly xi: reg msl_net freelunch i.teachergrade, cluster(gradeschoolunit) 
outreg, keep(freelunch) se summstat(N) store(row7) starlevels(10 5 1) starloc(1)
outreg, replay(tablez) append(row7) ctitles("", Coeff.) store(tablez) note("")

quietly xi: reg msl_net speced i.teachergrade, cluster(gradeschoolunit) 
outreg, keep(speced) se summstat(N) store(row8) starlevels(10 5 1) starloc(1)
outreg, replay(tablez) append(row8) ctitles("", Coeff.) store(tablez) note("")








*Used khan before (purpose)
quietly xi: reg msl_net zmss_21 i.teachergrade if classlu > 2, cluster(gradeschoolunit) 
outreg, keep(zmss_21) se summstat(N) store(row1) starlevels(10 5 1) starloc(1)
outreg, replay(tablea) append(row1) ctitles("", Coeff.) store(tablea) note("")

quietly xi: reg msl_net female i.teachergrade if classlu > 2, cluster(gradeschoolunit) 
outreg, keep(female) se summstat(N) store(row2) starlevels(10 5 1) starloc(1)
outreg, replay(tablea) append(row2) ctitles("", Coeff.) store(tablea) note("")

quietly xi: reg msl_net white i.teachergrade if classlu > 2, cluster(gradeschoolunit) 
outreg, keep(white) se summstat(N) store(row3) starlevels(10 5 1) starloc(1)
outreg, replay(tablea) append(row3) ctitles("", Coeff.) store(tablea) note("")

quietly xi: reg msl_net black i.teachergrade if classlu > 2, cluster(gradeschoolunit) 
outreg, keep(black) se summstat(N) store(row4) starlevels(10 5 1) starloc(1)
outreg, replay(tablea) append(row4) ctitles("", Coeff.) store(tablea) note("")

quietly xi: reg msl_net native i.teachergrade if classlu > 2, cluster(gradeschoolunit) 
outreg, keep(native) se summstat(N) store(row5) starlevels(10 5 1) starloc(1)
outreg, replay(tablea) append(row5) ctitles("", Coeff.) store(tablea) note("")

quietly xi: reg msl_net hispanic i.teachergrade if classlu > 2, cluster(gradeschoolunit) 
outreg, keep(hispanic) se summstat(N) store(row6) starlevels(10 5 1) starloc(1)
outreg, replay(tablea) append(row6) ctitles("", Coeff.) store(tablea) note("")

quietly xi: reg msl_net freelunch i.teachergrade if classlu > 2, cluster(gradeschoolunit) 
outreg, keep(freelunch) se summstat(N) store(row7) starlevels(10 5 1) starloc(1)
outreg, replay(tablea) append(row7) ctitles("", Coeff.) store(tablea) note("")

quietly xi: reg msl_net speced i.teachergrade if classlu > 2, cluster(gradeschoolunit) 
outreg, keep(speced) se summstat(N) store(row8) starlevels(10 5 1) starloc(1)
outreg, replay(tablea) append(row8) ctitles("", Coeff.) store(tablea) note("")


outreg, replay(tablez) merge(tablea)


outreg using "${outputpath}final_figures/msl_by_stud_char.tex", ///
    replay(tablez) tex fragment replace

	
	
	
	
	
	
	
	
	
	
	
	
	
		
********************************************************************************
*TABLE: Treatment effects by grade, 3 different specifications
use temp, clear
*	gen class_minutes3599 = classminutes>=35
	local gradecuts allgrades grade36
	local cuts all /**female male white black hispanic speced nonspec freelunch nofreelunch*/
	gen minutes3599 = total_minutes>=35
	replace class_minutes3599 = minutes3599
	label variable class_minutes3599 "35 or more minutes"
	
	local i = 1
	foreach gvar of local gradecuts {
		label variable all "`: var label `gvar''"
		foreach var of local cuts {
			ivregress 2sls zmss_22 (class_minutes3599 = treatment) if `var' == 1 & `gvar' == 1, cluster(gradeschoolunit) 
			outreg, keep(class_minutes3599) rtitle("`: var label `var''") se summstat(N) store(row`i') starlevels(10 5 1) starloc(1)
			outreg, replay(main2s) append(row`i') ctitles("", No Controls) store(main) note("")
			local ++i
		}

	}

	
	
	
	local i = 1
	foreach gvar of local gradecuts {
		label variable all "`: var label `gvar''"
		foreach var of local cuts {
			ivregress 2sls zmss_22 (class_minutes3599 = treatment) i.teachergrade if `var' == 1 & `gvar' == 1, cluster(gradeschoolunit) 
			outreg, keep(class_minutes3599) rtitle("`: var label `var''") se summstat(N) store(row`i') starlevels(10 5 1) starloc(1)
			outreg, replay(main12s) append(row`i') ctitles("", Grade FEs) store(main1) note("")
			local ++i
		}

	}

	
	local i = 1
	foreach gvar of local gradecuts {
		label variable all "`: var label `gvar''"
		foreach var of local cuts {
			ivregress 2sls zmss_22 (class_minutes3599 = treatment) ${controls1} i.teachergrade if `var' == 1 & `gvar' == 1, cluster(gradeschoolunit) 
			outreg, keep(class_minutes3599) rtitle("`: var label `var''") se summstat(N) store(row`i') starlevels(10 5 1) starloc(1)
			outreg, replay(main22s) append(row`i') ctitles("", Grade FEs/Student Controls) store(main2) note("")
			local ++i
		}

	}

	
	
	outreg, replay(main2s) merge(main12s)
outreg, replay(main2s) merge(main22s)
	
	
	outreg using "${outputpath}final_figures/main2s_analysis.tex", ///
    replay(main2s) tex fragment replace
	
	
	
	
	
	


/*
2) Student heterogeneity


For quasi experiment, include table looking at who are students that practice more and level up more

I think we mainly have previous test scores but we should also look at race, free lunch, etc…

Interact this with teacher class averages – it might be nice to show that in classrooms where more practice is happening, it's coming from lower SES and lower test score students

 

 

3) please keep working on getting the previous year's data, intermediate test scores for both last year and this year, and all the same data for this year. 
	
	
	
	
	
	
	

	
